-
Notifications
You must be signed in to change notification settings - Fork 560
Update if AlreadyExists #3202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update if AlreadyExists #3202
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,7 +150,10 @@ func (r *OperatorConditionReconciler) ensureOperatorConditionRole(operatorCondit | |
if !apierrors.IsNotFound(err) { | ||
return err | ||
} | ||
return r.Client.Create(context.TODO(), role) | ||
err = r.Client.Create(context.TODO(), role) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As in |
||
if apierrors.IsAlreadyExists(err) { | ||
return r.Client.Update(context.TODO(), role) | ||
} | ||
} | ||
|
||
if ownerutil.IsOwnedBy(existingRole, operatorCondition) && | ||
|
@@ -199,7 +202,10 @@ func (r *OperatorConditionReconciler) ensureOperatorConditionRoleBinding(operato | |
if !apierrors.IsNotFound(err) { | ||
return err | ||
} | ||
return r.Client.Create(context.TODO(), roleBinding) | ||
err = r.Client.Create(context.TODO(), roleBinding) | ||
if apierrors.IsAlreadyExists(err) { | ||
return r.Client.Update(context.TODO(), roleBinding) | ||
} | ||
} | ||
|
||
if ownerutil.IsOwnedBy(existingRoleBinding, operatorCondition) && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"time" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
@@ -92,7 +93,7 @@ func (c *Client) CreateCustomResourceRaw(apiGroup, version, namespace, kind stri | |
return nil | ||
} | ||
|
||
// CreateCustomResourceRawIfNotFound creates the raw bytes of the custom resource if it doesn't exist. | ||
// CreateCustomResourceRawIfNotFound creates the raw bytes of the custom resource if it doesn't exist, or Updates if it does exist. | ||
// It also returns a boolean to indicate whether a new custom resource is created. | ||
func (c *Client) CreateCustomResourceRawIfNotFound(apiGroup, version, namespace, kind, name string, data []byte) (bool, error) { | ||
klog.V(4).Infof("[CREATE CUSTOM RESOURCE RAW if not found]: %s:%s", namespace, name) | ||
|
@@ -104,7 +105,11 @@ func (c *Client) CreateCustomResourceRawIfNotFound(apiGroup, version, namespace, | |
return false, err | ||
} | ||
err = c.CreateCustomResourceRaw(apiGroup, version, namespace, kind, data) | ||
if err != nil { | ||
if apierrors.IsAlreadyExists(err) { | ||
if err = c.UpdateCustomResourceRaw(apiGroup, version, namespace, kind, name, data); err != nil { | ||
return false, err | ||
} | ||
} else if err != nil { | ||
return false, err | ||
} | ||
return true, nil | ||
Comment on lines
+112
to
115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit, This could be:
For most, if not all, of the returns. |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,15 @@ func (c *Client) GetDeployment(namespace, name string) (*appsv1.Deployment, erro | |
return c.AppsV1().Deployments(namespace).Get(context.TODO(), name, metav1.GetOptions{}) | ||
} | ||
|
||
// CreateDeployment creates the Deployment object. | ||
// CreateDeployment creates the Deployment object or Updates if it already exists. | ||
func (c *Client) CreateDeployment(dep *appsv1.Deployment) (*appsv1.Deployment, error) { | ||
klog.V(4).Infof("[CREATE Deployment]: %s:%s", dep.Namespace, dep.Name) | ||
return c.AppsV1().Deployments(dep.Namespace).Create(context.TODO(), dep, metav1.CreateOptions{}) | ||
createdDep, err := c.AppsV1().Deployments(dep.Namespace).Create(context.TODO(), dep, metav1.CreateOptions{}) | ||
if apierrors.IsAlreadyExists(err) { | ||
updatedDep, _, err := c.UpdateDeployment(dep) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Three returns? Compared to the others that just have two? (Just whining) |
||
return updatedDep, err | ||
} | ||
return createdDep, err | ||
} | ||
|
||
// DeleteDeployment deletes the Deployment object. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bundle_unpacker.go
does not have the wrapper client; I made the changes this way instead of spending the time to plumb the client in to keep the PR simpler and spend time on more important changes.