Skip to content

Commit

Permalink
use controller-runtime createorupdate method
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielXLee committed Jul 19, 2021
1 parent 0b057ac commit b5b8548
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rules:
- ippools
verbs:
- create
- get
- update
- apiGroups:
- discovery.k8s.io
Expand Down
24 changes: 11 additions & 13 deletions controllers/checker/handle_calico.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"text/template"

submarinerv1 "github.com/submariner-io/submariner/pkg/apis/submariner.io/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

Expand All @@ -43,10 +43,10 @@ func EnsureCalico(c client.Client, currentClusterID string, clusterInfos *[]brok
continue
}
cluster := getClusterWithID(clusterInfo.ClusterID, clusters)
if err := createOrUpdateIPPools(c, cluster.Spec.ClusterID+"-pod-cidr", cluster.Spec.ClusterCIDR[0]); err != nil {
if err := createOrUpdateIPPools(c, "pod-cidr-"+cluster.Spec.ClusterID, cluster.Spec.ClusterCIDR[0]); err != nil {
return err
}
if err := createOrUpdateIPPools(c, cluster.Spec.ClusterID+"-svc-cidr", cluster.Spec.ServiceCIDR[0]); err != nil {
if err := createOrUpdateIPPools(c, "svc-cidr-"+cluster.Spec.ClusterID, cluster.Spec.ServiceCIDR[0]); err != nil {
return err
}
}
Expand Down Expand Up @@ -98,6 +98,7 @@ func createOrUpdateIPPools(c client.Client, name, cidr string) error {
if err := t.Execute(&ippoolYaml, ippoolData); err != nil {
return err
}
klog.Infof("Create or update IPPool %s", name)
if err := createUpdateFromYaml(c, ippoolYaml.Bytes()); err != nil {
return err
}
Expand All @@ -116,17 +117,14 @@ func createUpdateFromYaml(c client.Client, yamlContent []byte) error {
klog.Errorf("could not unmarshal resource: %v", err)
return err
}
if err = c.Create(context.TODO(), obj); err != nil {
if errors.IsAlreadyExists(err) {
if err := c.Update(context.TODO(), obj); err != nil {
klog.Errorf("could not Update resource: %v", err)
return err
}
return nil
}
klog.Errorf("could not Create resource: %v", err)

or, err := ctrl.CreateOrUpdate(context.TODO(), c, obj, func() error {
return nil
})
if err != nil {
klog.Errorf("Failed to %s Object %s: %v", or, obj.GetName(), err)
return err
}

klog.Infof("Object %s %s", obj.GetName(), or)
return nil
}
2 changes: 1 addition & 1 deletion controllers/knitnet_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const (
// +kubebuilder:rbac:groups=operator.openshift.io,resources=dnses,verbs=get;list;watch;update

// Only for calico network plugin enabled
// +kubebuilder:rbac:groups=crd.projectcalico.org,resources=ippools,verbs=create;update
// +kubebuilder:rbac:groups=crd.projectcalico.org,resources=ippools,verbs=get;create;update

// +kubebuilder:rbac:groups=security.openshift.io,resources=securitycontextconstraints,verbs=get
// +kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;create
Expand Down

0 comments on commit b5b8548

Please sign in to comment.