diff --git a/pkg/k8sutil/unstructured.go b/pkg/k8sutil/unstructured.go index 347112c97..806482cb2 100644 --- a/pkg/k8sutil/unstructured.go +++ b/pkg/k8sutil/unstructured.go @@ -19,7 +19,6 @@ package k8sutil import ( "reflect" - "github.com/banzaicloud/istio-operator/pkg/k8sutil/objectmatch" "github.com/go-logr/logr" "github.com/goph/emperror" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -29,13 +28,14 @@ import ( "k8s.io/client-go/dynamic" istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" + "github.com/banzaicloud/istio-operator/pkg/k8sutil/objectmatch" ) type DesiredState string const ( - CREATED DesiredState = "created" - DELETED DesiredState = "deleted" + DesiredStatePresent DesiredState = "present" + DesiredStateAbsent DesiredState = "absent" ) type DynamicObject struct { @@ -49,6 +49,9 @@ type DynamicObject struct { } func (d *DynamicObject) Reconcile(log logr.Logger, client dynamic.Interface, desiredState DesiredState) error { + if desiredState == "" { + desiredState = DesiredStatePresent + } desired := d.unstructured() desiredType := reflect.TypeOf(desired) log = log.WithValues("type", reflect.TypeOf(d), "name", d.Name) @@ -56,18 +59,14 @@ func (d *DynamicObject) Reconcile(log logr.Logger, client dynamic.Interface, des if err != nil && !apierrors.IsNotFound(err) { return emperror.WrapWith(err, "getting resource failed", "name", d.Name, "kind", desiredType) } - if apierrors.IsNotFound(err) { - if desiredState == CREATED { - if _, err := client.Resource(d.Gvr).Namespace(d.Namespace).Create(desired, metav1.CreateOptions{}); err != nil { - return emperror.WrapWith(err, "creating resource failed", "name", d.Name, "kind", desiredType) - } - log.Info("resource created", "kind", d.Gvr.Resource) - } else if desiredState == DELETED { - log.Info("resource not found, already deleted", "kind", d.Gvr.Resource) + if apierrors.IsNotFound(err) && desiredState == DesiredStatePresent { + if _, err := client.Resource(d.Gvr).Namespace(d.Namespace).Create(desired, metav1.CreateOptions{}); err != nil { + return emperror.WrapWith(err, "creating resource failed", "name", d.Name, "kind", desiredType) } + log.Info("resource created", "kind", d.Gvr.Resource) } if err == nil { - if desiredState == CREATED { + if desiredState == DesiredStatePresent { objectsEquals, err := objectmatch.Match(current, desired) if err != nil { log.Error(err, "could not match objects", "kind", desiredType) @@ -81,7 +80,7 @@ func (d *DynamicObject) Reconcile(log logr.Logger, client dynamic.Interface, des return emperror.WrapWith(err, "updating resource failed", "name", d.Name, "kind", desiredType) } log.Info("resource updated", "kind", d.Gvr.Resource) - } else if desiredState == DELETED { + } else if desiredState == DesiredStateAbsent { if err := client.Resource(d.Gvr).Namespace(d.Namespace).Delete(d.Name, &metav1.DeleteOptions{}); err != nil { return emperror.WrapWith(err, "deleting resource failed", "name", d.Name, "kind", desiredType) } diff --git a/pkg/resources/citadel/citadel.go b/pkg/resources/citadel/citadel.go index ed94e61bb..416469767 100644 --- a/pkg/resources/citadel/citadel.go +++ b/pkg/resources/citadel/citadel.go @@ -17,13 +17,14 @@ limitations under the License. package citadel import ( - istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" - "github.com/banzaicloud/istio-operator/pkg/k8sutil" - "github.com/banzaicloud/istio-operator/pkg/resources" "github.com/go-logr/logr" "github.com/goph/emperror" "k8s.io/client-go/dynamic" "sigs.k8s.io/controller-runtime/pkg/client" + + istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" + "github.com/banzaicloud/istio-operator/pkg/k8sutil" + "github.com/banzaicloud/istio-operator/pkg/resources" ) const ( @@ -86,12 +87,12 @@ func (r *Reconciler) Reconcile(log logr.Logger) error { var mTLSDesiredState k8sutil.DesiredState if r.Config.Spec.MTLS { - mTLSDesiredState = k8sutil.CREATED + mTLSDesiredState = k8sutil.DesiredStatePresent } else { - mTLSDesiredState = k8sutil.DELETED + mTLSDesiredState = k8sutil.DesiredStateAbsent } drs := []resources.DynamicResourceWithDesiredState{ - {DynamicResource: r.meshPolicy, DesiredState: k8sutil.CREATED}, + {DynamicResource: r.meshPolicy, DesiredState: k8sutil.DesiredStatePresent}, {DynamicResource: r.destinationRuleDefaultMtls, DesiredState: mTLSDesiredState}, {DynamicResource: r.destinationRuleApiServerMtls, DesiredState: mTLSDesiredState}, } diff --git a/pkg/resources/mixer/mixer.go b/pkg/resources/mixer/mixer.go index 584cc800f..33268017b 100644 --- a/pkg/resources/mixer/mixer.go +++ b/pkg/resources/mixer/mixer.go @@ -19,13 +19,14 @@ package mixer import ( "fmt" - istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" - "github.com/banzaicloud/istio-operator/pkg/k8sutil" - "github.com/banzaicloud/istio-operator/pkg/resources" "github.com/go-logr/logr" "github.com/goph/emperror" "k8s.io/client-go/dynamic" "sigs.k8s.io/controller-runtime/pkg/client" + + istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" + "github.com/banzaicloud/istio-operator/pkg/k8sutil" + "github.com/banzaicloud/istio-operator/pkg/resources" ) const ( @@ -85,28 +86,28 @@ func (r *Reconciler) Reconcile(log logr.Logger) error { } } drs := []resources.DynamicResourceWithDesiredState{ - {DynamicResource: r.istioProxyAttributeManifest, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.kubernetesAttributeManifest, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.stdioHandler, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.accessLogLogentry, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.tcpAccessLogLogentry, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.stdioRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.stdioTcpRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.prometheusHandler, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.requestCountMetric, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.requestDurationMetric, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.requestSizeMetric, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.responseSizeMetric, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.tcpByteReceivedMetric, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.tcpByteSentMetric, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.promHttpRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.promTcpRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.kubernetesEnvHandler, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.attributesKubernetes, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.kubeAttrRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.tcpKubeAttrRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.policyDestinationRule, DesiredState: k8sutil.CREATED}, - {DynamicResource: r.telemetryDestinationRule, DesiredState: k8sutil.CREATED}, + {DynamicResource: r.istioProxyAttributeManifest}, + {DynamicResource: r.kubernetesAttributeManifest}, + {DynamicResource: r.stdioHandler}, + {DynamicResource: r.accessLogLogentry}, + {DynamicResource: r.tcpAccessLogLogentry}, + {DynamicResource: r.stdioRule}, + {DynamicResource: r.stdioTcpRule}, + {DynamicResource: r.prometheusHandler}, + {DynamicResource: r.requestCountMetric}, + {DynamicResource: r.requestDurationMetric}, + {DynamicResource: r.requestSizeMetric}, + {DynamicResource: r.responseSizeMetric}, + {DynamicResource: r.tcpByteReceivedMetric}, + {DynamicResource: r.tcpByteSentMetric}, + {DynamicResource: r.promHttpRule}, + {DynamicResource: r.promTcpRule}, + {DynamicResource: r.kubernetesEnvHandler}, + {DynamicResource: r.attributesKubernetes}, + {DynamicResource: r.kubeAttrRule}, + {DynamicResource: r.tcpKubeAttrRule}, + {DynamicResource: r.policyDestinationRule}, + {DynamicResource: r.telemetryDestinationRule}, } for _, dr := range drs { o := dr.DynamicResource() diff --git a/pkg/resources/pilot/pilot.go b/pkg/resources/pilot/pilot.go index a48bd1d10..9b13daddc 100644 --- a/pkg/resources/pilot/pilot.go +++ b/pkg/resources/pilot/pilot.go @@ -17,13 +17,14 @@ limitations under the License. package pilot import ( - istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" - "github.com/banzaicloud/istio-operator/pkg/k8sutil" - "github.com/banzaicloud/istio-operator/pkg/resources" "github.com/go-logr/logr" "github.com/goph/emperror" "k8s.io/client-go/dynamic" "sigs.k8s.io/controller-runtime/pkg/client" + + istiov1beta1 "github.com/banzaicloud/istio-operator/pkg/apis/istio/v1beta1" + "github.com/banzaicloud/istio-operator/pkg/k8sutil" + "github.com/banzaicloud/istio-operator/pkg/resources" ) const ( @@ -80,7 +81,7 @@ func (r *Reconciler) Reconcile(log logr.Logger) error { } } drs := []resources.DynamicResourceWithDesiredState{ - {DynamicResource: r.gateway, DesiredState: k8sutil.CREATED}, + {DynamicResource: r.gateway}, } for _, dr := range drs { o := dr.DynamicResource()