@@ -23,6 +23,8 @@ import (
23
23
"github.com/pkg/errors"
24
24
apierrors "k8s.io/apimachinery/pkg/api/errors"
25
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
+ "k8s.io/apimachinery/pkg/types"
27
+ "k8s.io/klog/v2"
26
28
"sigs.k8s.io/controller-runtime/pkg/client"
27
29
28
30
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -34,7 +36,7 @@ import (
34
36
// addClusterClassIfMissing returns a Template that includes the base template and adds any cluster class definitions that
35
37
// are references in the template. If the cluster class referenced already exists in the cluster it is not added to the
36
38
// 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 ) {
38
40
classes , err := clusterClassNamesFromTemplate (template )
39
41
if err != nil {
40
42
return nil , err
@@ -44,7 +46,7 @@ func addClusterClassIfMissing(ctx context.Context, template Template, clusterCla
44
46
return template , nil
45
47
}
46
48
47
- clusterClassesTemplate , err := fetchMissingClusterClassTemplates (ctx , clusterClassClient , clusterClient , classes , targetNamespace , listVariablesOnly )
49
+ clusterClassesTemplate , err := fetchMissingClusterClassTemplates (ctx , clusterClassClient , clusterClient , classes , listVariablesOnly )
48
50
if err != nil {
49
51
return nil , err
50
52
}
@@ -62,8 +64,8 @@ func addClusterClassIfMissing(ctx context.Context, template Template, clusterCla
62
64
// clusterClassNamesFromTemplate returns the list of ClusterClasses referenced
63
65
// by clusters defined in the template. If not clusters are defined in the template
64
66
// 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 {}
67
69
68
70
// loop through all the objects and if the object is a cluster
69
71
// check and see if cluster.spec.topology.class is defined.
@@ -80,14 +82,14 @@ func clusterClassNamesFromTemplate(template Template) ([]string, error) {
80
82
if cluster .Spec .Topology == nil {
81
83
continue
82
84
}
83
- classes = append (classes , cluster .GetClassKey (). Name )
85
+ classes = append (classes , cluster .GetClassKey ())
84
86
}
85
87
return classes , nil
86
88
}
87
89
88
90
// fetchMissingClusterClassTemplates returns a list of templates for ClusterClasses that do not yet exist
89
91
// 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 ) {
91
93
// first check if the cluster is initialized.
92
94
// If it is initialized:
93
95
// For every ClusterClass check if it already exists in the cluster.
@@ -118,7 +120,7 @@ func fetchMissingClusterClassTemplates(ctx context.Context, clusterClassClient r
118
120
templates := []repository.Template {}
119
121
for _ , class := range classes {
120
122
if clusterInitialized {
121
- exists , err := clusterClassExists (ctx , c , class , targetNamespace )
123
+ exists , err := clusterClassExists (ctx , c , class . Name , class . Namespace )
122
124
if err != nil {
123
125
return nil , err
124
126
}
@@ -128,7 +130,7 @@ func fetchMissingClusterClassTemplates(ctx context.Context, clusterClassClient r
128
130
}
129
131
// The cluster is either not initialized or the ClusterClass does not yet exist in the cluster.
130
132
// 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 )
132
134
if err != nil {
133
135
return nil , errors .Wrapf (err , "failed to get the cluster class template for %q" , class )
134
136
}
@@ -142,7 +144,7 @@ func fetchMissingClusterClassTemplates(ctx context.Context, clusterClassClient r
142
144
if exists , err := objExists (ctx , c , obj ); err != nil {
143
145
return nil , err
144
146
} 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 ())
146
148
}
147
149
}
148
150
}
0 commit comments