Skip to content
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

Use built in isExists #942

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
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
15 changes: 3 additions & 12 deletions pkg/kudoctl/cmd/init/crds.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package init

import (
"errors"
"strings"

"github.com/kudobuilder/kudo/pkg/kudoctl/clog"
Expand Down Expand Up @@ -35,26 +34,18 @@ func installCrds(client apiextensionsclient.Interface) error {
func installOperator(client v1beta1.CustomResourceDefinitionsGetter) error {
o := generateOperator()
_, err := client.CustomResourceDefinitions().Create(o)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("crd %v already exists", o.Name)
return nil
}
return err

}

func isAlreadyExistsError(err error) bool {
var statusError *kerrors.StatusError
if errors.As(err, &statusError) {
return statusError.ErrStatus.Reason == "AlreadyExists"
}
return false
}

func installOperatorVersion(client v1beta1.CustomResourceDefinitionsGetter) error {
ov := generateOperatorVersion()
_, err := client.CustomResourceDefinitions().Create(ov)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("crd %v already exists", ov.Name)
return nil
}
Expand All @@ -64,7 +55,7 @@ func installOperatorVersion(client v1beta1.CustomResourceDefinitionsGetter) erro
func installInstance(client v1beta1.CustomResourceDefinitionsGetter) error {
instance := generateInstance()
_, err := client.CustomResourceDefinitions().Create(instance)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("crd %v already exists", instance.Name)
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/kudoctl/cmd/init/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -93,7 +94,7 @@ func installManager(client kubernetes.Interface, opts Options) error {
func installStatefulSet(client appsv1client.StatefulSetsGetter, opts Options) error {
ss := generateDeployment(opts)
_, err := client.StatefulSets(opts.Namespace).Create(ss)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("statefulset %v already exists", ss.Name)
return nil
}
Expand All @@ -103,7 +104,7 @@ func installStatefulSet(client appsv1client.StatefulSetsGetter, opts Options) er
func installService(client corev1.ServicesGetter, opts Options) error {
s := generateService(opts)
_, err := client.Services(opts.Namespace).Create(s)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("service %v already exists", s.Name)
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/kudoctl/cmd/init/prereqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -35,7 +36,7 @@ func installPrereqs(client kubernetes.Interface, opts Options) error {
func installSecret(client corev1.SecretsGetter, opts Options) error {
secret := generateWebHookSecret(opts)
_, err := client.Secrets(opts.Namespace).Create(secret)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("secret %v already exists", secret.Name)
return nil
}
Expand All @@ -45,7 +46,7 @@ func installSecret(client corev1.SecretsGetter, opts Options) error {
func installRoleBindings(client kubernetes.Interface, opts Options) error {
rbac := generateRoleBinding(opts)
_, err := client.RbacV1().ClusterRoleBindings().Create(rbac)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("role binding %v already exists", rbac.Name)
return nil
}
Expand All @@ -55,7 +56,7 @@ func installRoleBindings(client kubernetes.Interface, opts Options) error {
func installNamespace(client corev1.NamespacesGetter, opts Options) error {
ns := generateSysNamespace(opts.Namespace)
_, err := client.Namespaces().Create(ns)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("namespace %v already exists", ns.Name)
return nil
}
Expand All @@ -65,7 +66,7 @@ func installNamespace(client corev1.NamespacesGetter, opts Options) error {
func installServiceAccount(client corev1.ServiceAccountsGetter, opts Options) error {
sa := generateServiceAccount(opts)
_, err := client.ServiceAccounts(opts.Namespace).Create(sa)
if isAlreadyExistsError(err) {
if kerrors.IsAlreadyExists(err) {
clog.V(4).Printf("service account %v already exists", sa.Name)
return nil
}
Expand Down