@@ -21,6 +21,7 @@ import (
21
21
pmemtls "github.com/intel/pmem-csi/pkg/pmem-csi-operator/pmem-tls"
22
22
pmemgrpc "github.com/intel/pmem-csi/pkg/pmem-grpc"
23
23
"github.com/intel/pmem-csi/pkg/version"
24
+
24
25
appsv1 "k8s.io/api/apps/v1"
25
26
corev1 "k8s.io/api/core/v1"
26
27
rbacv1 "k8s.io/api/rbac/v1"
@@ -45,23 +46,60 @@ const (
45
46
provisionerMetricsPort = 10011
46
47
)
47
48
49
+ func typeMeta (gv schema.GroupVersion , kind string ) metav1.TypeMeta {
50
+ return metav1.TypeMeta {
51
+ APIVersion : gv .String (),
52
+ Kind : kind ,
53
+ }
54
+ }
55
+
56
+ // A list of all currently created objects. This must be kept in sync
57
+ // with the code in Reconcile(). When removing a type here, it must be
58
+ // copied to obsoleteObjects below.
59
+ //
60
+ // The RBAC rules in deploy/kustomize/operator/operator.yaml must
61
+ // allow all of the operations (creation, patching, etc.).
62
+ var currentObjects = []apiruntime.Object {
63
+ & rbacv1.ClusterRole {TypeMeta : typeMeta (rbacv1 .SchemeGroupVersion , "ClusterRole" )},
64
+ & rbacv1.ClusterRoleBinding {TypeMeta : typeMeta (rbacv1 .SchemeGroupVersion , "ClusterRoleBinding" )},
65
+ & storagev1beta1.CSIDriver {TypeMeta : typeMeta (storagev1beta1 .SchemeGroupVersion , "CSIDriver" )},
66
+ & appsv1.DaemonSet {TypeMeta : typeMeta (appsv1 .SchemeGroupVersion , "DaemonSet" )},
67
+ & rbacv1.Role {TypeMeta : typeMeta (rbacv1 .SchemeGroupVersion , "Role" )},
68
+ & rbacv1.RoleBinding {TypeMeta : typeMeta (rbacv1 .SchemeGroupVersion , "RoleBinding" )},
69
+ & corev1.Secret {TypeMeta : typeMeta (corev1 .SchemeGroupVersion , "Secret" )},
70
+ & corev1.Service {TypeMeta : typeMeta (corev1 .SchemeGroupVersion , "Service" )},
71
+ & corev1.ServiceAccount {TypeMeta : typeMeta (corev1 .SchemeGroupVersion , "ServiceAccount" )},
72
+ & appsv1.StatefulSet {TypeMeta : typeMeta (appsv1 .SchemeGroupVersion , "StatefulSet" )},
73
+ }
74
+
75
+ // A list of objects that may have been created by a previous release
76
+ // of the operator. This is relevant when updating from such an older
77
+ // release to the current one, because the current one must remove
78
+ // obsolete objects.
79
+ //
80
+ // The RBAC rules in deploy/kustomize/operator/operator.yaml must
81
+ // allow listing and removing of these objects.
82
+ var obsoleteObjects = []apiruntime.Object {
83
+ & corev1.ConfigMap {TypeMeta : typeMeta (corev1 .SchemeGroupVersion , "ConfigMap" )}, // included only for testing purposes
84
+ }
85
+
48
86
// A list of all object types potentially created by the operator,
49
87
// in this or any previous release. In other words, this list may grow,
50
- // but never shrink, because a newer release needs to delete objects
51
- // created by an older release.
52
- // This list also must be kept in sync with the operator RBAC rules.
53
- var AllObjectTypes = []schema. GroupVersionKind {
54
- rbacv1 . SchemeGroupVersion . WithKind ( "RoleList" ),
55
- rbacv1 . SchemeGroupVersion . WithKind ( "ClusterRoleList" ),
56
- rbacv1 . SchemeGroupVersion . WithKind ( "RoleBindingList" ),
57
- rbacv1 . SchemeGroupVersion . WithKind ( "ClusterRoleBindingList" ),
58
- corev1 . SchemeGroupVersion . WithKind ( "ServiceAccountList" ),
59
- corev1 . SchemeGroupVersion . WithKind ( "SecretList" ),
60
- corev1 . SchemeGroupVersion . WithKind ( "ServiceList" ),
61
- corev1 . SchemeGroupVersion . WithKind ( "ConfigMapList" ),
62
- appsv1 . SchemeGroupVersion . WithKind ( "DaemonSetList" ),
63
- appsv1 . SchemeGroupVersion . WithKind ( "StatefulSetList" ),
64
- storagev1beta1 . SchemeGroupVersion . WithKind ( "CSIDriverList" ),
88
+ // but never shrink.
89
+ var allObjects = append ( currentObjects [:], obsoleteObjects ... )
90
+
91
+ // Returns a slice with a new unstructured.UnstructuredList for each object
92
+ // in allObjects.
93
+ func AllObjectLists () [] * unstructured. UnstructuredList {
94
+ var lists [] * unstructured. UnstructuredList
95
+ for _ , obj := range allObjects {
96
+ gvk := obj . GetObjectKind (). GroupVersionKind ()
97
+ gvk . Kind += "List"
98
+ list := & unstructured. UnstructuredList {}
99
+ list . SetGroupVersionKind ( gvk )
100
+ lists = append ( lists , list )
101
+ }
102
+ return lists
65
103
}
66
104
67
105
type PmemCSIDriver struct {
@@ -150,7 +188,9 @@ func (op *ObjectPatch) Apply(c client.Client, labels map[string]string) error {
150
188
return c .Patch (context .TODO (), op .obj , op .patch )
151
189
}
152
190
153
- // Reconcile reconciles the driver deployment
191
+ // Reconcile reconciles the driver deployment. When adding new
192
+ // objects, extend also currentObjects above and the RBAC rules in
193
+ // deploy/kustomize/operator/operator.yaml.
154
194
func (d * PmemCSIDriver ) Reconcile (r * ReconcileDeployment ) error {
155
195
156
196
if err := d .EnsureDefaults (r .containerImage ); err != nil {
@@ -638,14 +678,12 @@ func (d *PmemCSIDriver) deleteObsoleteObjects(r *ReconcileDeployment, newObjects
638
678
klog .V (5 ).Infof ("==>%q type %q" , metaObj .GetName (), obj .GetObjectKind ().GroupVersionKind ())
639
679
}
640
680
641
- for _ , gvk := range AllObjectTypes {
642
- list := & unstructured.UnstructuredList {}
643
- list .SetGroupVersionKind (gvk )
681
+ for _ , list := range AllObjectLists () {
644
682
opts := & client.ListOptions {
645
683
Namespace : d .namespace ,
646
684
}
647
685
648
- klog .V (5 ).Infof ("Fetching '%s' list with options: %v" , gvk , opts .Namespace )
686
+ klog .V (5 ).Infof ("Fetching '%s' list with options: %v" , list . GetObjectKind () , opts .Namespace )
649
687
if err := r .client .List (context .TODO (), list , opts ); err != nil {
650
688
return err
651
689
}
0 commit comments