|
| 1 | +package certrotationcontroller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 9 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 10 | + |
| 11 | + "github.com/openshift/library-go/pkg/controller/factory" |
| 12 | + "github.com/openshift/library-go/pkg/operator/certrotation" |
| 13 | + "github.com/openshift/library-go/pkg/operator/condition" |
| 14 | + "github.com/openshift/library-go/pkg/operator/events" |
| 15 | + "github.com/openshift/library-go/pkg/operator/v1helpers" |
| 16 | +) |
| 17 | + |
| 18 | +type OperatorConditionStatusReporter struct { |
| 19 | + // Plumbing: |
| 20 | + OperatorClient v1helpers.OperatorClient |
| 21 | +} |
| 22 | + |
| 23 | +func (s *OperatorConditionStatusReporter) Report(ctx context.Context, controllerName string, syncErr error) (bool, error) { |
| 24 | + newCondition := operatorv1.OperatorCondition{ |
| 25 | + Type: fmt.Sprintf(condition.CertRotationDegradedConditionTypeFmt, controllerName), |
| 26 | + Status: operatorv1.ConditionFalse, |
| 27 | + } |
| 28 | + if syncErr != nil { |
| 29 | + newCondition.Status = operatorv1.ConditionTrue |
| 30 | + newCondition.Reason = "CertificateRotationError" |
| 31 | + newCondition.Message = syncErr.Error() |
| 32 | + } |
| 33 | + _, updated, updateErr := v1helpers.UpdateStatus(ctx, s.OperatorClient, v1helpers.UpdateConditionFn(newCondition)) |
| 34 | + return updated, updateErr |
| 35 | +} |
| 36 | + |
| 37 | +type CertRotationController struct { |
| 38 | + certRotators []factory.Controller |
| 39 | +} |
| 40 | + |
| 41 | +func NewCertRotationController( |
| 42 | + secretsGetter corev1client.SecretsGetter, |
| 43 | + configMapsGetter corev1client.ConfigMapsGetter, |
| 44 | + operatorClient v1helpers.OperatorClient, |
| 45 | + kubeInformersForNamespaces v1helpers.KubeInformersForNamespaces, |
| 46 | + eventRecorder events.Recorder, |
| 47 | + day time.Duration, |
| 48 | +) (*CertRotationController, error) { |
| 49 | + ret := &CertRotationController{} |
| 50 | + |
| 51 | + targetNS := "openshift-oauth-apiserver" |
| 52 | + |
| 53 | + certRotator := certrotation.NewCertRotationController( |
| 54 | + "OAuthLoopbackCert", |
| 55 | + certrotation.RotatedSigningCASecret{ |
| 56 | + Namespace: targetNS, |
| 57 | + Name: "loopback-signer", |
| 58 | + AdditionalAnnotations: certrotation.AdditionalAnnotations{ |
| 59 | + JiraComponent: "oauth-apiserver", |
| 60 | + }, |
| 61 | + Validity: 60 * day, |
| 62 | + Refresh: 30 * day, |
| 63 | + RefreshOnlyWhenExpired: false, |
| 64 | + Informer: kubeInformersForNamespaces.InformersFor(targetNS).Core().V1().Secrets(), |
| 65 | + Lister: kubeInformersForNamespaces.InformersFor(targetNS).Core().V1().Secrets().Lister(), |
| 66 | + Client: secretsGetter, |
| 67 | + EventRecorder: eventRecorder, |
| 68 | + }, |
| 69 | + certrotation.CABundleConfigMap{ |
| 70 | + Namespace: targetNS, |
| 71 | + Name: "loopback-ca", |
| 72 | + AdditionalAnnotations: certrotation.AdditionalAnnotations{ |
| 73 | + JiraComponent: "oauth-apiserver", |
| 74 | + }, |
| 75 | + Informer: kubeInformersForNamespaces.InformersFor(targetNS).Core().V1().ConfigMaps(), |
| 76 | + Lister: kubeInformersForNamespaces.InformersFor(targetNS).Core().V1().ConfigMaps().Lister(), |
| 77 | + Client: configMapsGetter, |
| 78 | + EventRecorder: eventRecorder, |
| 79 | + }, |
| 80 | + certrotation.RotatedSelfSignedCertKeySecret{ |
| 81 | + Namespace: targetNS, |
| 82 | + Name: "loopback", |
| 83 | + AdditionalAnnotations: certrotation.AdditionalAnnotations{ |
| 84 | + JiraComponent: "oauth-apiserver", |
| 85 | + }, |
| 86 | + Validity: 30 * day, |
| 87 | + Refresh: 15 * day, |
| 88 | + RefreshOnlyWhenExpired: false, |
| 89 | + CertCreator: &certrotation.ServingRotation{ |
| 90 | + Hostnames: func() []string { return []string{"apiserver-loopback-client"} }, |
| 91 | + }, |
| 92 | + Informer: kubeInformersForNamespaces.InformersFor(targetNS).Core().V1().Secrets(), |
| 93 | + Lister: kubeInformersForNamespaces.InformersFor(targetNS).Core().V1().Secrets().Lister(), |
| 94 | + Client: secretsGetter, |
| 95 | + EventRecorder: eventRecorder, |
| 96 | + }, |
| 97 | + eventRecorder, |
| 98 | + &OperatorConditionStatusReporter{OperatorClient: operatorClient}, |
| 99 | + ) |
| 100 | + |
| 101 | + ret.certRotators = append(ret.certRotators, certRotator) |
| 102 | + |
| 103 | + return ret, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (c *CertRotationController) Run(ctx context.Context, workers int) { |
| 107 | + syncCtx := context.WithValue(ctx, certrotation.RunOnceContextKey, false) |
| 108 | + for _, certRotator := range c.certRotators { |
| 109 | + go certRotator.Run(syncCtx, workers) |
| 110 | + } |
| 111 | +} |
0 commit comments