From 69f32200c953d497350a77426dac0cb6ff9d0478 Mon Sep 17 00:00:00 2001 From: Nitish Tiwari Date: Wed, 13 Jan 2021 18:59:09 +0530 Subject: [PATCH] Multiple fixes for operator plugin - Improve error message if CRD is not created. - Improve prompt during upgrade. - Create the webhook service so that operator webhook server is accessible via DNS. --- kubectl-minio/cmd/init.go | 16 +++++++++ kubectl-minio/cmd/resources/service.go | 45 ++++++++++++++++++++++++++ kubectl-minio/cmd/tenant-upgrade.go | 7 ++-- kubectl-minio/cmd/tenant.go | 2 +- 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 kubectl-minio/cmd/resources/service.go diff --git a/kubectl-minio/cmd/init.go b/kubectl-minio/cmd/init.go index 31020df2739..edc551cf7f9 100644 --- a/kubectl-minio/cmd/init.go +++ b/kubectl-minio/cmd/init.go @@ -86,6 +86,7 @@ func (o *operatorInitCmd) run() error { sa := resources.NewServiceAccountForOperator(helpers.DefaultServiceAccount, o.operatorOpts.NS) crb := resources.NewCluterRoleBindingForOperator(helpers.DefaultServiceAccount, o.operatorOpts.NS) d := resources.NewDeploymentForOperator(o.operatorOpts) + svc := resources.NewServiceForOperator(o.operatorOpts) if !o.output { path, _ := rootCmd.Flags().GetString(kubeconfig) @@ -109,6 +110,9 @@ func (o *operatorInitCmd) run() error { if err = createClusterRB(client, crb); err != nil { return err } + if err = createService(client, svc); err != nil { + return err + } return createDeployment(client, d) } @@ -183,3 +187,15 @@ func createDeployment(client *kubernetes.Clientset, d *appsv1.Deployment) error fmt.Printf("MinIO Operator Deployment %s: created\n", d.ObjectMeta.Name) return nil } + +func createService(client *kubernetes.Clientset, svc *corev1.Service) error { + _, err := client.CoreV1().Services(svc.ObjectMeta.Namespace).Create(context.Background(), svc, v1.CreateOptions{}) + if err != nil { + if k8serrors.IsAlreadyExists(err) { + return fmt.Errorf("MinIO Operator Service %s: already present, skipped", svc.ObjectMeta.Name) + } + return err + } + fmt.Printf("MinIO Operator Service %s: created\n", svc.ObjectMeta.Name) + return nil +} diff --git a/kubectl-minio/cmd/resources/service.go b/kubectl-minio/cmd/resources/service.go new file mode 100644 index 00000000000..8943c006bfa --- /dev/null +++ b/kubectl-minio/cmd/resources/service.go @@ -0,0 +1,45 @@ +/* + * This file is part of MinIO Operator + * Copyright (C) 2021, MinIO, Inc. + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +package resources + +import ( + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// NewServiceForOperator will return a new service for a MinIO Operator webhook server +func NewServiceForOperator(opts OperatorOptions) *corev1.Service { + operatorWebhookHTTPPort := corev1.ServicePort{Port: 4222, Name: "http"} + operatorWebhookHTTPSPort := corev1.ServicePort{Port: 4233, Name: "https"} + return &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Labels: operatorLabels(), + Name: "operator", + Namespace: opts.NS, + }, + Spec: corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ + operatorWebhookHTTPPort, + operatorWebhookHTTPSPort, + }, + Selector: operatorLabels(), + Type: corev1.ServiceTypeClusterIP, + }, + } +} diff --git a/kubectl-minio/cmd/tenant-upgrade.go b/kubectl-minio/cmd/tenant-upgrade.go index 4c55aa906b9..476a30a882c 100644 --- a/kubectl-minio/cmd/tenant-upgrade.go +++ b/kubectl-minio/cmd/tenant-upgrade.go @@ -126,8 +126,6 @@ func (u *upgradeCmd) run() error { u.tenantOpts.Name, u.tenantOpts.Image, t.Spec.Image) } - // update the image - t.Spec.Image = u.tenantOpts.Image if u.tenantOpts.ImagePullSecret != "" { t.Spec.ImagePullSecret = corev1.LocalObjectReference{Name: u.tenantOpts.ImagePullSecret} } @@ -135,7 +133,8 @@ func (u *upgradeCmd) run() error { if !u.output { return u.upgradeTenant(client, t, t.Spec.Image, u.tenantOpts.Image) } - + // update the image + t.Spec.Image = u.tenantOpts.Image o, err := yaml.Marshal(&t) if err != nil { return err @@ -147,6 +146,8 @@ func (u *upgradeCmd) run() error { func (u *upgradeCmd) upgradeTenant(client *operatorv1.Clientset, t *miniov1.Tenant, c, p string) error { if helpers.Ask(fmt.Sprintf("Upgrade is a one way process. Are you sure to upgrade Tenant '%s/%s' from version %s to %s?", t.ObjectMeta.Name, t.ObjectMeta.Namespace, c, p)) { fmt.Printf(color.Bold(fmt.Sprintf("\nUpgrading Tenant '%s/%s'\n\n", t.ObjectMeta.Name, t.ObjectMeta.Namespace))) + // update the image + t.Spec.Image = u.tenantOpts.Image if _, err := client.MinioV1().Tenants(t.Namespace).Update(context.Background(), t, v1.UpdateOptions{}); err != nil { return err } diff --git a/kubectl-minio/cmd/tenant.go b/kubectl-minio/cmd/tenant.go index bf334120108..c2c2648a344 100644 --- a/kubectl-minio/cmd/tenant.go +++ b/kubectl-minio/cmd/tenant.go @@ -47,7 +47,7 @@ func newTenantCmd(out io.Writer, errOut io.Writer) *cobra.Command { _, err = client.ApiextensionsV1beta1().CustomResourceDefinitions().Get(context.Background(), crdObj.GetObjectMeta().GetName(), v1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { - return fmt.Errorf("CustomResourceDefinition %s: not found, please run 'kubectl minio operator create' before using tenant command", crdObj.ObjectMeta.Name) + return fmt.Errorf("CustomResourceDefinition %s: not found, please run 'kubectl minio init' before using tenant command", crdObj.ObjectMeta.Name) } return err }