Skip to content

Commit

Permalink
Merge pull request #74 from banzaicloud/desired-state
Browse files Browse the repository at this point in the history
Enhance desired resource state handling
  • Loading branch information
martonsereg authored Mar 1, 2019
2 parents 66e0cd0 + b4db7c1 commit 1de9c31
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 48 deletions.
25 changes: 12 additions & 13 deletions pkg/k8sutil/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -49,25 +49,24 @@ 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)
current, err := client.Resource(d.Gvr).Namespace(d.Namespace).Get(d.Name, metav1.GetOptions{})
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)
Expand All @@ -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)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/resources/citadel/citadel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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},
}
Expand Down
51 changes: 26 additions & 25 deletions pkg/resources/mixer/mixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 5 additions & 4 deletions pkg/resources/pilot/pilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 1de9c31

Please sign in to comment.