@@ -23,6 +23,8 @@ import (
2323"github.com/pkg/errors"
2424apierrors "k8s.io/apimachinery/pkg/api/errors"
2525"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26+ "k8s.io/apimachinery/pkg/types"
27+ "k8s.io/klog/v2"
2628"sigs.k8s.io/controller-runtime/pkg/client"
2729
2830clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -34,7 +36,7 @@ import (
3436// addClusterClassIfMissing returns a Template that includes the base template and adds any cluster class definitions that
3537// are references in the template. If the cluster class referenced already exists in the cluster it is not added to the
3638// template.
37- func addClusterClassIfMissing (ctx context.Context , template Template , clusterClassClient repository.ClusterClassClient , clusterClient cluster.Client , targetNamespace string , listVariablesOnly bool ) (Template , error ) {
39+ func addClusterClassIfMissing (ctx context.Context , template Template , clusterClassClient repository.ClusterClassClient , clusterClient cluster.Client , listVariablesOnly bool ) (Template , error ) {
3840classes , err := clusterClassNamesFromTemplate (template )
3941if err != nil {
4042return nil , err
@@ -44,7 +46,7 @@ func addClusterClassIfMissing(ctx context.Context, template Template, clusterCla
4446return template , nil
4547}
4648
47- clusterClassesTemplate , err := fetchMissingClusterClassTemplates (ctx , clusterClassClient , clusterClient , classes , targetNamespace , listVariablesOnly )
49+ clusterClassesTemplate , err := fetchMissingClusterClassTemplates (ctx , clusterClassClient , clusterClient , classes , listVariablesOnly )
4850if err != nil {
4951return nil , err
5052}
@@ -62,8 +64,8 @@ func addClusterClassIfMissing(ctx context.Context, template Template, clusterCla
6264// clusterClassNamesFromTemplate returns the list of ClusterClasses referenced
6365// by clusters defined in the template. If not clusters are defined in the template
6466// or if no cluster uses a cluster class it returns an empty list.
65- func clusterClassNamesFromTemplate (template Template ) ([]string , error ) {
66- classes := []string {}
67+ func clusterClassNamesFromTemplate (template Template ) ([]types. NamespacedName , error ) {
68+ classes := []types. NamespacedName {}
6769
6870// loop through all the objects and if the object is a cluster
6971// check and see if cluster.spec.topology.class is defined.
@@ -80,14 +82,14 @@ func clusterClassNamesFromTemplate(template Template) ([]string, error) {
8082if cluster .Spec .Topology == nil {
8183continue
8284}
83- classes = append (classes , cluster .GetClassKey (). Name )
85+ classes = append (classes , cluster .GetClassKey ())
8486}
8587return classes , nil
8688}
8789
8890// fetchMissingClusterClassTemplates returns a list of templates for ClusterClasses that do not yet exist
8991// in the cluster. If the cluster is not initialized, all the ClusterClasses are added.
90- func fetchMissingClusterClassTemplates (ctx context.Context , clusterClassClient repository.ClusterClassClient , clusterClient cluster.Client , classes []string , targetNamespace string , listVariablesOnly bool ) (Template , error ) {
92+ func fetchMissingClusterClassTemplates (ctx context.Context , clusterClassClient repository.ClusterClassClient , clusterClient cluster.Client , classes []types. NamespacedName , listVariablesOnly bool ) (Template , error ) {
9193// first check if the cluster is initialized.
9294// If it is initialized:
9395// For every ClusterClass check if it already exists in the cluster.
@@ -118,7 +120,7 @@ func fetchMissingClusterClassTemplates(ctx context.Context, clusterClassClient r
118120templates := []repository.Template {}
119121for _ , class := range classes {
120122if clusterInitialized {
121- exists , err := clusterClassExists (ctx , c , class , targetNamespace )
123+ exists , err := clusterClassExists (ctx , c , class . Name , class . Namespace )
122124if err != nil {
123125return nil , err
124126}
@@ -128,7 +130,7 @@ func fetchMissingClusterClassTemplates(ctx context.Context, clusterClassClient r
128130}
129131// The cluster is either not initialized or the ClusterClass does not yet exist in the cluster.
130132// Fetch the cluster class to install.
131- clusterClassTemplate , err := clusterClassClient .Get (ctx , class , targetNamespace , listVariablesOnly )
133+ clusterClassTemplate , err := clusterClassClient .Get (ctx , class . Name , class . Namespace , listVariablesOnly )
132134if err != nil {
133135return nil , errors .Wrapf (err , "failed to get the cluster class template for %q" , class )
134136}
@@ -142,7 +144,7 @@ func fetchMissingClusterClassTemplates(ctx context.Context, clusterClassClient r
142144if exists , err := objExists (ctx , c , obj ); err != nil {
143145return nil , err
144146} else if exists {
145- return nil , fmt .Errorf ("%s(%s) already exists in the cluster" , obj . GetName ( ), obj .GetObjectKind ().GroupVersionKind ())
147+ return nil , fmt .Errorf ("%s(%s) already exists in the cluster" , klog . KObj ( & obj ), obj .GetObjectKind ().GroupVersionKind ())
146148}
147149}
148150}
0 commit comments