Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/clusterctl/clusterdeployer/clusterclient/clusterclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ func (c *client) EnsureNamespace(namespaceName string) error {
return errors.Wrap(err, "error creating core clientset")
}

_, err = clientset.CoreV1().Namespaces().Get(namespaceName, metav1.GetOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about restructuring it like this?

// Happy path - return quickly
if err == nil {
  return nil
}

if apierrors.IsForbidden(err) {
  // get was forbidden. see if list works

  namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
  if apierrors.IsForbidden(err) {
    // either return err, which means we halt work
    // or return nil, and either things will be ok (user has permissions to create CAPI resources but they can't check for namespace existence), or things won't (they can't create CAPI resources, or the namespace doesn't exist)
  }
  if err != nil {
    return err
  }
  // check the list
}

if !apierrors.IsNotFound(err) {
  // err is not Forbidden or NotFound - return it
  return err
}

// try to create
// ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ncdc I'm sorry for long delay. Thank you so much for the proposal. Could you please take a look again.

if err == nil {
return nil
}
if apierrors.IsForbidden(err) {
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return err
}

for _, ns := range namespaces.Items {
if ns.Name == namespaceName {
return nil
}
}
}
if !apierrors.IsNotFound(err) {
return err
}
namespace := apiv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
Expand Down