devopscredential_controller.go 10.9 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2020 KubeSphere Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

R
runzexia 已提交
17 18 19
package devopscredential

import (
H
hongming 已提交
20
	"context"
R
runzexia 已提交
21
	"fmt"
22
	"github.com/emicklei/go-restful"
R
runzexia 已提交
23 24
	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
H
hongming 已提交
25
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
R
runzexia 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/util/wait"
	corev1informer "k8s.io/client-go/informers/core/v1"
	clientset "k8s.io/client-go/kubernetes"
	"k8s.io/client-go/kubernetes/scheme"
	v1core "k8s.io/client-go/kubernetes/typed/core/v1"
	corev1lister "k8s.io/client-go/listers/core/v1"
	"k8s.io/client-go/tools/cache"
	"k8s.io/client-go/tools/record"
	"k8s.io/client-go/util/workqueue"
	"k8s.io/klog"
	devopsv1alpha3 "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3"
	kubesphereclient "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
	"kubesphere.io/kubesphere/pkg/constants"
40
	"kubesphere.io/kubesphere/pkg/controller/utils"
S
shaowenchen 已提交
41
	modelsdevops "kubesphere.io/kubesphere/pkg/models/devops"
R
runzexia 已提交
42 43 44
	devopsClient "kubesphere.io/kubesphere/pkg/simple/client/devops"
	"kubesphere.io/kubesphere/pkg/utils/k8sutil"
	"kubesphere.io/kubesphere/pkg/utils/sliceutil"
45
	"net/http"
R
runzexia 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	"reflect"
	"strings"
	"time"
)

/**
  DevOps project controller is used to maintain the state of the DevOps project.
*/

type Controller struct {
	client           clientset.Interface
	kubesphereClient kubesphereclient.Interface

	eventBroadcaster record.EventBroadcaster
	eventRecorder    record.EventRecorder

	secretLister corev1lister.SecretLister
	secretSynced cache.InformerSynced

	namespaceLister corev1lister.NamespaceLister
	namespaceSynced cache.InformerSynced

	workqueue workqueue.RateLimitingInterface

	workerLoopPeriod time.Duration

	devopsClient devopsClient.Interface
}

func NewController(client clientset.Interface,
	devopsClinet devopsClient.Interface,
	namespaceInformer corev1informer.NamespaceInformer,
	secretInformer corev1informer.SecretInformer) *Controller {

	broadcaster := record.NewBroadcaster()
	broadcaster.StartLogging(func(format string, args ...interface{}) {
		klog.Info(fmt.Sprintf(format, args))
	})
	broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: client.CoreV1().Events("")})
R
runzexia 已提交
85
	recorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "devopscredential-controller"})
R
runzexia 已提交
86 87 88 89

	v := &Controller{
		client:           client,
		devopsClient:     devopsClinet,
R
runzexia 已提交
90
		workqueue:        workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "devopscredential"),
R
runzexia 已提交
91 92 93 94 95 96 97 98 99 100 101 102
		secretLister:     secretInformer.Lister(),
		secretSynced:     secretInformer.Informer().HasSynced,
		namespaceLister:  namespaceInformer.Lister(),
		namespaceSynced:  namespaceInformer.Informer().HasSynced,
		workerLoopPeriod: time.Second,
	}

	v.eventBroadcaster = broadcaster
	v.eventRecorder = recorder

	secretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
		AddFunc: func(obj interface{}) {
R
runzexia 已提交
103 104
			secret, ok := obj.(*v1.Secret)
			if ok && strings.HasPrefix(string(secret.Type), devopsv1alpha3.DevOpsCredentialPrefix) {
R
runzexia 已提交
105 106 107 108
				v.enqueueSecret(obj)
			}
		},
		UpdateFunc: func(oldObj, newObj interface{}) {
R
runzexia 已提交
109 110 111
			old, ook := oldObj.(*v1.Secret)
			new, nok := newObj.(*v1.Secret)
			if ook && nok && old.ResourceVersion == new.ResourceVersion {
R
runzexia 已提交
112 113
				return
			}
R
runzexia 已提交
114
			if ook && nok && strings.HasPrefix(string(new.Type), devopsv1alpha3.DevOpsCredentialPrefix) {
R
runzexia 已提交
115 116 117 118
				v.enqueueSecret(newObj)
			}
		},
		DeleteFunc: func(obj interface{}) {
R
runzexia 已提交
119 120
			secret, ok := obj.(*v1.Secret)
			if ok && strings.HasPrefix(string(secret.Type), devopsv1alpha3.DevOpsCredentialPrefix) {
R
runzexia 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
				v.enqueueSecret(obj)
			}
		},
	})
	return v
}

// enqueueSecret takes a Foo resource and converts it into a namespace/name
// string which is then put onto the work workqueue. This method should *not* be
// passed resources of any type other than DevOpsProject.
func (c *Controller) enqueueSecret(obj interface{}) {
	var key string
	var err error
	if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
		utilruntime.HandleError(err)
		return
	}
	c.workqueue.Add(key)
}

func (c *Controller) processNextWorkItem() bool {
	obj, shutdown := c.workqueue.Get()

	if shutdown {
		return false
	}

	err := func(obj interface{}) error {
		defer c.workqueue.Done(obj)
		var key string
		var ok bool

		if key, ok = obj.(string); !ok {
			c.workqueue.Forget(obj)
			utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
			return nil
		}
		if err := c.syncHandler(key); err != nil {
			c.workqueue.AddRateLimited(key)
			return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
		}
		c.workqueue.Forget(obj)
		klog.V(5).Infof("Successfully synced '%s'", key)
		return nil
	}(obj)

	if err != nil {
		klog.Error(err, "could not reconcile devopsProject")
		utilruntime.HandleError(err)
		return true
	}

	return true
}

func (c *Controller) worker() {

	for c.processNextWorkItem() {
	}
}

func (c *Controller) Start(stopCh <-chan struct{}) error {
	return c.Run(1, stopCh)
}

func (c *Controller) Run(workers int, stopCh <-chan struct{}) error {
	defer utilruntime.HandleCrash()
	defer c.workqueue.ShutDown()

R
runzexia 已提交
190 191
	klog.Info("starting devopscredential controller")
	defer klog.Info("shutting down  devopscredential controller")
R
runzexia 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205

	if !cache.WaitForCacheSync(stopCh, c.secretSynced) {
		return fmt.Errorf("failed to wait for caches to sync")
	}

	for i := 0; i < workers; i++ {
		go wait.Until(c.worker, c.workerLoopPeriod, stopCh)
	}

	<-stopCh
	return nil
}

// syncHandler compares the actual state with the desired, and attempts to
R
runzexia 已提交
206
// converge the two. It then updates the Status block of the secret resource
R
runzexia 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
// with the current status of the resource.
func (c *Controller) syncHandler(key string) error {
	nsName, name, err := cache.SplitMetaNamespaceKey(key)
	if err != nil {
		klog.Error(err, fmt.Sprintf("could not split copySecret meta %s ", key))
		return nil
	}
	namespace, err := c.namespaceLister.Get(nsName)
	if err != nil {
		if errors.IsNotFound(err) {
			klog.Info(fmt.Sprintf("namespace '%s' in work queue no longer exists ", key))
			return nil
		}
		klog.Error(err, fmt.Sprintf("could not get namespace %s ", key))
		return err
	}
	if !isDevOpsProjectAdminNamespace(namespace) {
224
		err := fmt.Errorf("cound not create or update credential '%s' in normal namespaces %s", name, namespace.Name)
R
runzexia 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		klog.Warning(err)
		return err
	}

	secret, err := c.secretLister.Secrets(nsName).Get(name)
	if err != nil {
		if errors.IsNotFound(err) {
			klog.Info(fmt.Sprintf("secret '%s' in work queue no longer exists ", key))
			return nil
		}
		klog.Error(err, fmt.Sprintf("could not get secret %s ", key))
		return err
	}

	copySecret := secret.DeepCopy()
	// DeletionTimestamp.IsZero() means copySecret has not been deleted.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	if copySecret.ObjectMeta.DeletionTimestamp.IsZero() {
		// make sure Annotations is not nil
		if copySecret.Annotations == nil {
			copySecret.Annotations = map[string]string{}
		}

		//If the sync is successful, return handle
		if state, ok := copySecret.Annotations[devopsv1alpha3.CredentialSyncStatusAnnoKey]; ok && state == modelsdevops.StatusSuccessful {
			specHash := utils.ComputeHash(copySecret.Data)
			oldHash, _ := copySecret.Annotations[devopsv1alpha3.DevOpsCredentialDataHash] // don't need to check if it's nil, only compare if they're different
			if specHash == oldHash {
				// it was synced successfully, and there's any change with the Pipeline spec, skip this round
				return nil
			} else {
				copySecret.Annotations[devopsv1alpha3.DevOpsCredentialDataHash] = specHash
			}
		}

R
runzexia 已提交
259
		// https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#finalizers
S
shaowenchen 已提交
260
		if !sliceutil.HasString(secret.ObjectMeta.Finalizers, devopsv1alpha3.CredentialFinalizerName) {
R
runzexia 已提交
261 262 263 264
			copySecret.ObjectMeta.Finalizers = append(copySecret.ObjectMeta.Finalizers, devopsv1alpha3.CredentialFinalizerName)
		}
		// Check secret config exists, otherwise we will create it.
		// if secret exists, update config
S
shaowenchen 已提交
265 266
		_, err := c.devopsClient.GetCredentialInProject(nsName, copySecret.Name)
		if err == nil {
R
runzexia 已提交
267 268 269
			if _, ok := copySecret.Annotations[devopsv1alpha3.CredentialAutoSyncAnnoKey]; ok {
				_, err := c.devopsClient.UpdateCredentialInProject(nsName, copySecret)
				if err != nil {
S
shaowenchen 已提交
270
					klog.V(8).Info(err, fmt.Sprintf("failed to update secret %s ", key))
R
runzexia 已提交
271 272 273
					return err
				}
			}
S
shaowenchen 已提交
274 275 276 277 278 279
		} else {
			_, err = c.devopsClient.CreateCredentialInProject(nsName, copySecret)
			if err != nil {
				klog.V(8).Info(err, fmt.Sprintf("failed to create secret %s ", key))
				return err
			}
R
runzexia 已提交
280
		}
S
shaowenchen 已提交
281 282
		//If there is no early return, then the sync is successful.
		copySecret.Annotations[devopsv1alpha3.CredentialSyncStatusAnnoKey] = modelsdevops.StatusSuccessful
R
runzexia 已提交
283 284 285
	} else {
		// Finalizers processing logic
		if sliceutil.HasString(copySecret.ObjectMeta.Finalizers, devopsv1alpha3.CredentialFinalizerName) {
286
			delSuccess := false
S
shaowenchen 已提交
287
			if _, err := c.devopsClient.DeleteCredentialInProject(nsName, secret.Name); err != nil {
288 289 290 291 292 293 294 295 296
				// the status code should be 404 if the credential does not exists
				if srvErr, ok := err.(restful.ServiceError); ok {
					delSuccess = srvErr.Code == http.StatusNotFound
				} else if srvErr, ok := err.(*devopsClient.ErrorResponse); ok {
					delSuccess = srvErr.Response.StatusCode == http.StatusNotFound
				} else {
					klog.Error(fmt.Sprintf("unexpected error type: %v, should be *restful.ServiceError", err))
				}

S
shaowenchen 已提交
297
				klog.V(8).Info(err, fmt.Sprintf("failed to delete secret %s in devops", key))
298 299 300 301 302 303 304 305 306 307 308 309
			} else {
				delSuccess = true
			}

			if delSuccess {
				copySecret.ObjectMeta.Finalizers = sliceutil.RemoveString(copySecret.ObjectMeta.Finalizers, func(item string) bool {
					return item == devopsv1alpha3.CredentialFinalizerName
				})
			} else {
				// make sure the corresponding Jenkins credentials can be clean
				// You can remove the finalizer via kubectl manually in a very special case that Jenkins might be not able to available anymore
				return fmt.Errorf("failed to remove devops credential finalizer due to bad communication with Jenkins")
R
runzexia 已提交
310 311 312 313 314
			}

		}
	}
	if !reflect.DeepEqual(secret, copySecret) {
H
hongming 已提交
315
		_, err = c.client.CoreV1().Secrets(nsName).Update(context.Background(), copySecret, metav1.UpdateOptions{})
R
runzexia 已提交
316
		if err != nil {
S
shaowenchen 已提交
317
			klog.V(8).Info(err, fmt.Sprintf("failed to update secret %s ", key))
R
runzexia 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330
			return err
		}
	}
	return nil
}

func isDevOpsProjectAdminNamespace(namespace *v1.Namespace) bool {
	_, ok := namespace.Labels[constants.DevOpsProjectLabelKey]

	return ok && k8sutil.IsControlledBy(namespace.OwnerReferences,
		devopsv1alpha3.ResourceKindDevOpsProject, "")

}