@@ -15,15 +15,13 @@ import (
15
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
16
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
17
17
"k8s.io/apimachinery/pkg/runtime"
18
+ "k8s.io/utils/pointer"
18
19
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
19
20
"sigs.k8s.io/controller-runtime/pkg/client"
20
21
"sigs.k8s.io/yaml"
21
22
)
22
23
23
- // CNIForCluster returns a complete set of Cluster API objects to describe a CNI Configuration
24
- // installable via Flux resources.
25
- func CNIForCluster (cluster * clusterv1.Cluster ) ([]unstructured.Unstructured , error ) {
26
- vals , _ := yaml .YAMLToJSON ([]byte (`
24
+ var calicoHelmReleaseValues , _ = yaml .YAMLToJSON ([]byte (`
27
25
installation:
28
26
cni:
29
27
type: Calico
@@ -40,6 +38,8 @@ installation:
40
38
typhaMetricsPort: 9093
41
39
` ))
42
40
41
+ // CNIForCluster returns a set of objects to describe a CNI Configuration installable via Flux resources.
42
+ func CNIForCluster (cluster * clusterv1.Cluster ) ([]unstructured.Unstructured , error ) {
43
43
objs := []client.Object {
44
44
& corev1.Namespace {
45
45
TypeMeta : metav1.TypeMeta {
@@ -63,54 +63,27 @@ installation:
63
63
URL : "https://docs.tigera.io/calico/charts" ,
64
64
},
65
65
},
66
- & fluxhelmv2beta1.HelmRelease {
67
- TypeMeta : metav1.TypeMeta {
68
- APIVersion : fluxhelmv2beta1 .GroupVersion .String (),
69
- Kind : fluxhelmv2beta1 .HelmReleaseKind ,
70
- },
71
- ObjectMeta : metav1.ObjectMeta {
72
- Name : cluster .Name + "-tigera-operator" ,
73
- Namespace : cluster .Namespace ,
74
- },
75
- Spec : fluxhelmv2beta1.HelmReleaseSpec {
76
- KubeConfig : & fluxhelmv2beta1.KubeConfig {
77
- SecretRef : meta.SecretKeyReference {
78
- Name : fmt .Sprintf ("%s-kubeconfig" , cluster .Name ),
79
- Key : "value" ,
80
- },
81
- },
82
- TargetNamespace : "tigera-operator" ,
83
- ReleaseName : "tigera-operator" ,
84
- Chart : fluxhelmv2beta1.HelmChartTemplate {
85
- Spec : fluxhelmv2beta1.HelmChartTemplateSpec {
86
- SourceRef : fluxhelmv2beta1.CrossNamespaceObjectReference {
87
- APIVersion : fluxsourcev1beta2 .GroupVersion .String (),
88
- Kind : fluxsourcev1beta2 .HelmRepositoryKind ,
89
- Namespace : "flux-helmrelease-addons" ,
90
- Name : "projectcalico" ,
91
- },
92
- Chart : "tigera-operator" ,
93
- Version : " v3.25.0" ,
94
- },
95
- },
96
- Values : & apiextensionsv1.JSON {Raw : vals },
97
- Install : & fluxhelmv2beta1.Install {
98
- CreateNamespace : true ,
99
- CRDs : fluxhelmv2beta1 .CreateReplace ,
100
- Remediation : & fluxhelmv2beta1.InstallRemediation {
101
- Retries : 30 ,
102
- },
103
- },
104
- Upgrade : & fluxhelmv2beta1.Upgrade {
105
- CRDs : fluxhelmv2beta1 .CreateReplace ,
106
- Remediation : & fluxhelmv2beta1.UpgradeRemediation {
107
- Retries : 30 ,
108
- },
109
- },
110
- },
111
- },
66
+ calicoHelmReleaseForCluster (cluster ),
67
+ }
68
+
69
+ unstrObjs := make ([]unstructured.Unstructured , 0 , len (objs ))
70
+ for _ , obj := range objs {
71
+ unstrObj , err := runtime .DefaultUnstructuredConverter .ToUnstructured (obj )
72
+ if err != nil {
73
+ return nil , err
74
+ }
75
+ unstrObjs = append (unstrObjs , unstructured.Unstructured {Object : unstrObj })
112
76
}
113
77
78
+ return unstrObjs , nil
79
+ }
80
+
81
+ // CNIPatchesForClusterDelete returns a set of patches to apply before cluster deletion.
82
+ func CNIPatchesForClusterDelete (cluster * clusterv1.Cluster ) ([]unstructured.Unstructured , error ) {
83
+ hr := calicoHelmReleaseForCluster (cluster )
84
+ hr .Spec .Suspend = true
85
+ objs := []client.Object {hr }
86
+
114
87
unstrObjs := make ([]unstructured.Unstructured , 0 , len (objs ))
115
88
for _ , obj := range objs {
116
89
unstrObj , err := runtime .DefaultUnstructuredConverter .ToUnstructured (obj )
@@ -122,3 +95,59 @@ installation:
122
95
123
96
return unstrObjs , nil
124
97
}
98
+
99
+ func calicoHelmReleaseForCluster (cluster * clusterv1.Cluster ) * fluxhelmv2beta1.HelmRelease {
100
+ return & fluxhelmv2beta1.HelmRelease {
101
+ TypeMeta : metav1.TypeMeta {
102
+ APIVersion : fluxhelmv2beta1 .GroupVersion .String (),
103
+ Kind : fluxhelmv2beta1 .HelmReleaseKind ,
104
+ },
105
+ ObjectMeta : metav1.ObjectMeta {
106
+ Name : cluster .Name + "-tigera-operator" ,
107
+ Namespace : cluster .Namespace ,
108
+ OwnerReferences : []metav1.OwnerReference {{
109
+ APIVersion : cluster .APIVersion ,
110
+ Kind : cluster .Kind ,
111
+ Name : cluster .Name ,
112
+ UID : cluster .UID ,
113
+ Controller : pointer .Bool (true ),
114
+ }},
115
+ },
116
+ Spec : fluxhelmv2beta1.HelmReleaseSpec {
117
+ KubeConfig : & fluxhelmv2beta1.KubeConfig {
118
+ SecretRef : meta.SecretKeyReference {
119
+ Name : fmt .Sprintf ("%s-kubeconfig" , cluster .Name ),
120
+ Key : "value" ,
121
+ },
122
+ },
123
+ TargetNamespace : "tigera-operator" ,
124
+ ReleaseName : "tigera-operator" ,
125
+ Chart : fluxhelmv2beta1.HelmChartTemplate {
126
+ Spec : fluxhelmv2beta1.HelmChartTemplateSpec {
127
+ SourceRef : fluxhelmv2beta1.CrossNamespaceObjectReference {
128
+ APIVersion : fluxsourcev1beta2 .GroupVersion .String (),
129
+ Kind : fluxsourcev1beta2 .HelmRepositoryKind ,
130
+ Namespace : "flux-helmrelease-addons" ,
131
+ Name : "projectcalico" ,
132
+ },
133
+ Chart : "tigera-operator" ,
134
+ Version : " v3.25.0" ,
135
+ },
136
+ },
137
+ Values : & apiextensionsv1.JSON {Raw : calicoHelmReleaseValues },
138
+ Install : & fluxhelmv2beta1.Install {
139
+ CreateNamespace : true ,
140
+ CRDs : fluxhelmv2beta1 .CreateReplace ,
141
+ Remediation : & fluxhelmv2beta1.InstallRemediation {
142
+ Retries : 30 ,
143
+ },
144
+ },
145
+ Upgrade : & fluxhelmv2beta1.Upgrade {
146
+ CRDs : fluxhelmv2beta1 .CreateReplace ,
147
+ Remediation : & fluxhelmv2beta1.UpgradeRemediation {
148
+ Retries : 30 ,
149
+ },
150
+ },
151
+ },
152
+ }
153
+ }
0 commit comments