Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 7a8e83d

Browse files
committed
operator: remove dead code
With recent changes deployment CR comparsion code is no more needed.
1 parent 272e560 commit 7a8e83d

File tree

3 files changed

+0
-263
lines changed

3 files changed

+0
-263
lines changed

pkg/apis/pmemcsi/v1alpha1/deployment_types.go

-115
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ SPDX-License-Identifier: Apache-2.0
77
package v1alpha1
88

99
import (
10-
"bytes"
1110
"errors"
1211
"fmt"
13-
"reflect"
1412
"strings"
1513

1614
corev1 "k8s.io/api/core/v1"
@@ -279,51 +277,6 @@ const (
279277
DeploymentPhaseFailed DeploymentPhase = "Failed"
280278
)
281279

282-
// DeploymentChange type declaration for changes between two deployments
283-
type DeploymentChange int
284-
285-
const (
286-
DriverMode = iota + 1
287-
DriverImage
288-
PullPolicy
289-
LogLevel
290-
ProvisionerImage
291-
NodeRegistrarImage
292-
ControllerResources
293-
NodeResources
294-
NodeSelector
295-
PMEMPercentage
296-
Labels
297-
CACertificate
298-
RegistryCertificate
299-
RegistryKey
300-
NodeControllerCertificate
301-
NodeControllerKey
302-
KubeletDir
303-
)
304-
305-
func (c DeploymentChange) String() string {
306-
return map[DeploymentChange]string{
307-
DriverMode: "deviceMode",
308-
DriverImage: "image",
309-
PullPolicy: "imagePullPolicy",
310-
LogLevel: "logLevel",
311-
ProvisionerImage: "provisionerImage",
312-
NodeRegistrarImage: "nodeRegistrarImage",
313-
ControllerResources: "controllerResources",
314-
NodeResources: "nodeResources",
315-
NodeSelector: "nodeSelector",
316-
PMEMPercentage: "pmemPercentage",
317-
Labels: "labels",
318-
CACertificate: "caCert",
319-
RegistryCertificate: "registryCert",
320-
RegistryKey: "registryKey",
321-
NodeControllerCertificate: "nodeControllerCert",
322-
NodeControllerKey: "nodeControllerKey",
323-
KubeletDir: "kubeletDir",
324-
}[c]
325-
}
326-
327280
func (d *Deployment) SetCondition(t DeploymentConditionType, state corev1.ConditionStatus, reason string) {
328281
for _, c := range d.Status.Conditions {
329282
if c.Type == t {
@@ -426,74 +379,6 @@ func (d *Deployment) EnsureDefaults(operatorImage string) error {
426379
return nil
427380
}
428381

429-
// Compare compares 'other' deployment spec with current deployment and returns
430-
// the all the changes. If len(changes) == 0 represents both deployment spec
431-
// are equivalent.
432-
func (d *Deployment) Compare(other *Deployment) map[DeploymentChange]struct{} {
433-
changes := map[DeploymentChange]struct{}{}
434-
if d == nil || other == nil {
435-
return changes
436-
}
437-
438-
if d.Spec.DeviceMode != other.Spec.DeviceMode {
439-
changes[DriverMode] = struct{}{}
440-
}
441-
if d.Spec.Image != other.Spec.Image {
442-
changes[DriverImage] = struct{}{}
443-
}
444-
if d.Spec.PullPolicy != other.Spec.PullPolicy {
445-
changes[PullPolicy] = struct{}{}
446-
}
447-
if d.Spec.LogLevel != other.Spec.LogLevel {
448-
changes[LogLevel] = struct{}{}
449-
}
450-
if d.Spec.ProvisionerImage != other.Spec.ProvisionerImage {
451-
changes[ProvisionerImage] = struct{}{}
452-
}
453-
if d.Spec.NodeRegistrarImage != other.Spec.NodeRegistrarImage {
454-
changes[NodeRegistrarImage] = struct{}{}
455-
}
456-
if !compareResources(d.Spec.ControllerResources, other.Spec.ControllerResources) {
457-
changes[ControllerResources] = struct{}{}
458-
}
459-
if !compareResources(d.Spec.NodeResources, other.Spec.NodeResources) {
460-
changes[NodeResources] = struct{}{}
461-
}
462-
463-
if !reflect.DeepEqual(d.Spec.NodeSelector, other.Spec.NodeSelector) {
464-
changes[NodeSelector] = struct{}{}
465-
}
466-
467-
if d.Spec.PMEMPercentage != other.Spec.PMEMPercentage {
468-
changes[PMEMPercentage] = struct{}{}
469-
}
470-
471-
if !reflect.DeepEqual(d.Spec.Labels, other.Spec.Labels) {
472-
changes[Labels] = struct{}{}
473-
}
474-
475-
if bytes.Compare(d.Spec.CACert, other.Spec.CACert) != 0 {
476-
changes[CACertificate] = struct{}{}
477-
}
478-
if bytes.Compare(d.Spec.RegistryCert, other.Spec.RegistryCert) != 0 {
479-
changes[RegistryCertificate] = struct{}{}
480-
}
481-
if bytes.Compare(d.Spec.NodeControllerCert, other.Spec.NodeControllerCert) != 0 {
482-
changes[NodeControllerCertificate] = struct{}{}
483-
}
484-
if bytes.Compare(d.Spec.RegistryPrivateKey, other.Spec.RegistryPrivateKey) != 0 {
485-
changes[RegistryKey] = struct{}{}
486-
}
487-
if bytes.Compare(d.Spec.NodeControllerPrivateKey, other.Spec.NodeControllerPrivateKey) != 0 {
488-
changes[NodeControllerKey] = struct{}{}
489-
}
490-
if d.Spec.KubeletDir != other.Spec.KubeletDir {
491-
changes[KubeletDir] = struct{}{}
492-
}
493-
494-
return changes
495-
}
496-
497382
// GetHyphenedName returns the name of the deployment with dots replaced by hyphens.
498383
// Most objects created for the deployment will use hyphens in the name, sometimes
499384
// with an additional suffix like -controller, but others must use the original

pkg/apis/pmemcsi/v1alpha1/deployment_types_test.go

-84
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
api "github.com/intel/pmem-csi/pkg/apis/pmemcsi/v1alpha1"
1313
. "github.com/onsi/ginkgo"
1414
. "github.com/onsi/gomega"
15-
corev1 "k8s.io/api/core/v1"
1615
"k8s.io/apimachinery/pkg/api/resource"
1716
"k8s.io/client-go/kubernetes/scheme"
1817
)
@@ -107,89 +106,6 @@ spec:
107106
Expect(nrs.Memory().Cmp(resource.MustParse("100Mi"))).Should(BeZero(), "node driver 'cpu' resource mismatch")
108107
})
109108

110-
It("compare two deployments", func() {
111-
d1 := &api.Deployment{}
112-
d2 := &api.Deployment{}
113-
changes := map[api.DeploymentChange]struct{}{}
114-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "two empty deployments should be equal")
115-
116-
err := d1.EnsureDefaults("")
117-
Expect(err).ShouldNot(HaveOccurred(), "ensure defaults")
118-
err = d2.EnsureDefaults("")
119-
Expect(err).ShouldNot(HaveOccurred(), "ensure defaults")
120-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "two default deployments should be equaval")
121-
122-
d2.Spec.DeviceMode = api.DeviceModeDirect
123-
changes[api.DriverMode] = struct{}{}
124-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect chagned device mode")
125-
126-
d2.Spec.LogLevel = d2.Spec.LogLevel + 1
127-
changes[api.LogLevel] = struct{}{}
128-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in log level")
129-
130-
d2.Spec.Image = "new-driver-image"
131-
changes[api.DriverImage] = struct{}{}
132-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in driver image")
133-
134-
d2.Spec.PullPolicy = corev1.PullNever
135-
changes[api.PullPolicy] = struct{}{}
136-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in image pull policy")
137-
138-
d2.Spec.ProvisionerImage = "new-provisioner-image"
139-
changes[api.ProvisionerImage] = struct{}{}
140-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in provisioner image")
141-
142-
d2.Spec.NodeRegistrarImage = "new-node-driver-registrar-image"
143-
changes[api.NodeRegistrarImage] = struct{}{}
144-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node registrar image")
145-
146-
d2.Spec.ControllerResources = &corev1.ResourceRequirements{}
147-
changes[api.ControllerResources] = struct{}{}
148-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in controller resources")
149-
150-
d2.Spec.ControllerResources = &corev1.ResourceRequirements{
151-
Requests: corev1.ResourceList{
152-
corev1.ResourceCPU: resource.MustParse("10m"),
153-
corev1.ResourceMemory: resource.MustParse("5Gi"),
154-
},
155-
}
156-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in controller resource requests")
157-
158-
d2.Spec.ControllerResources = &corev1.ResourceRequirements{
159-
Limits: corev1.ResourceList{
160-
corev1.ResourceCPU: resource.MustParse("10m"),
161-
corev1.ResourceMemory: resource.MustParse("5Gi"),
162-
},
163-
}
164-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in controller resource limits")
165-
166-
d2.Spec.NodeResources = &corev1.ResourceRequirements{}
167-
changes[api.NodeResources] = struct{}{}
168-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node resources")
169-
170-
d2.Spec.NodeResources = &corev1.ResourceRequirements{
171-
Limits: corev1.ResourceList{
172-
corev1.ResourceCPU: resource.MustParse("10m"),
173-
corev1.ResourceMemory: resource.MustParse("5Gi"),
174-
},
175-
}
176-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node resource limits")
177-
178-
d2.Spec.NodeResources = &corev1.ResourceRequirements{
179-
Requests: corev1.ResourceList{
180-
corev1.ResourceCPU: resource.MustParse("10m"),
181-
corev1.ResourceMemory: resource.MustParse("5Gi"),
182-
},
183-
}
184-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node resource requests")
185-
186-
d2.Spec.NodeSelector = map[string]string{
187-
"new-label": "value",
188-
}
189-
changes[api.NodeSelector] = struct{}{}
190-
Expect(d1.Compare(d2)).Should(BeElementOf(changes), "expected to detect change in node selector")
191-
})
192-
193109
It("should have valid json schema", func() {
194110
crd := api.GetDeploymentCRDSchema()
195111
Expect(crd).ShouldNot(BeNil(), "Nil CRD schmea")

pkg/pmem-csi-operator/controller/deployment/deployment_controller.go

-64
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
corev1 "k8s.io/api/core/v1"
2323
rbacv1 "k8s.io/api/rbac/v1"
2424
storagev1beta1 "k8s.io/api/storage/v1beta1"
25-
"k8s.io/apimachinery/pkg/api/errors"
2625
"k8s.io/apimachinery/pkg/api/meta"
2726
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2827
"k8s.io/apimachinery/pkg/runtime"
@@ -370,69 +369,6 @@ func (r *ReconcileDeployment) Get(obj runtime.Object) error {
370369
return r.client.Get(context.TODO(), key, obj)
371370
}
372371

373-
// Create create new Kubernetes object
374-
func (r *ReconcileDeployment) Create(obj runtime.Object) error {
375-
metaObj, err := meta.Accessor(obj)
376-
if err != nil {
377-
return fmt.Errorf("internal error %T: %v", obj, err)
378-
}
379-
klog.Infof("Creating: '%s/%s' of type %T", metaObj.GetNamespace(), metaObj.GetName(), obj)
380-
return r.client.Create(context.TODO(), obj)
381-
}
382-
383-
// Update updates existing Kubernetes object. The object must be a modified copy of the existing object in the apiserver.
384-
func (r *ReconcileDeployment) Update(obj runtime.Object) error {
385-
metaObj, err := meta.Accessor(obj)
386-
if err != nil {
387-
return fmt.Errorf("internal error %T: %v", obj, err)
388-
}
389-
klog.Infof("Updating '%s/%s' of type '%T'", metaObj.GetNamespace(), metaObj.GetName(), obj)
390-
return r.client.Update(context.TODO(), obj)
391-
}
392-
393-
// UpdateOrCreate updates the spec of an existing object or, if it does not exist yet, creates it.
394-
func (r *ReconcileDeployment) UpdateOrCreate(obj runtime.Object) error {
395-
metaObj, err := meta.Accessor(obj)
396-
if err != nil {
397-
return fmt.Errorf("internal error %T: %v", obj, err)
398-
}
399-
existing := obj.DeepCopyObject()
400-
err = r.Get(existing)
401-
if err != nil && !errors.IsNotFound(err) {
402-
return err
403-
}
404-
if err == nil {
405-
metaExisting, err := meta.Accessor(existing)
406-
if err != nil {
407-
return fmt.Errorf("internal error %T: %v", existing, err)
408-
}
409-
410-
ownerRef := metaObj.GetOwnerReferences()[0]
411-
if !isOwnedBy(metaExisting, &ownerRef) {
412-
return fmt.Errorf("'%s' of type %T is not owned by '%s'", metaObj.GetName(), obj, ownerRef.Name)
413-
}
414-
415-
// Copy metadata from existing object
416-
metaObj.SetGenerateName(metaExisting.GetGenerateName())
417-
metaObj.SetSelfLink(metaExisting.GetSelfLink())
418-
metaObj.SetUID(metaExisting.GetUID())
419-
metaObj.SetResourceVersion(metaExisting.GetResourceVersion())
420-
metaObj.SetGeneration(metaExisting.GetGeneration())
421-
metaObj.SetCreationTimestamp(metaExisting.GetCreationTimestamp())
422-
metaObj.SetAnnotations(metaExisting.GetAnnotations())
423-
metaObj.SetFinalizers(metaExisting.GetFinalizers())
424-
metaObj.SetClusterName(metaExisting.GetClusterName())
425-
metaObj.SetManagedFields(metaExisting.GetManagedFields())
426-
metaObj.SetLabels(joinMaps(metaExisting.GetLabels(), metaObj.GetLabels()))
427-
428-
klog.Infof("Updating '%s/%s' of type '%T'", metaObj.GetNamespace(), metaObj.GetName(), obj)
429-
return r.client.Update(context.TODO(), obj)
430-
}
431-
// Fall back to creating the object.
432-
klog.Infof("Creating '%s/%s' of type '%T'", metaObj.GetNamespace(), metaObj.GetName(), obj)
433-
return r.client.Create(context.TODO(), obj)
434-
}
435-
436372
// Delete delete existing Kubernetes object
437373
func (r *ReconcileDeployment) Delete(obj runtime.Object) error {
438374
metaObj, err := meta.Accessor(obj)

0 commit comments

Comments
 (0)