|
| 1 | +package oauthclients |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + corev1informers "k8s.io/client-go/informers/core/v1" |
| 11 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 12 | + corev1listers "k8s.io/client-go/listers/core/v1" |
| 13 | + "k8s.io/klog/v2" |
| 14 | + |
| 15 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 16 | + configv1informers "github.com/openshift/client-go/config/informers/externalversions/config/v1" |
| 17 | + configv1lister "github.com/openshift/client-go/config/listers/config/v1" |
| 18 | + oauthv1client "github.com/openshift/client-go/oauth/clientset/versioned/typed/oauth/v1" |
| 19 | + oauthv1informers "github.com/openshift/client-go/oauth/informers/externalversions/oauth/v1" |
| 20 | + oauthv1lister "github.com/openshift/client-go/oauth/listers/oauth/v1" |
| 21 | + operatorv1informers "github.com/openshift/client-go/operator/informers/externalversions/operator/v1" |
| 22 | + operatorv1listers "github.com/openshift/client-go/operator/listers/operator/v1" |
| 23 | + routev1informers "github.com/openshift/client-go/route/informers/externalversions/route/v1" |
| 24 | + routev1listers "github.com/openshift/client-go/route/listers/route/v1" |
| 25 | + "github.com/openshift/library-go/pkg/controller/factory" |
| 26 | + "github.com/openshift/library-go/pkg/operator/events" |
| 27 | + "github.com/openshift/library-go/pkg/operator/resource/resourceapply" |
| 28 | + "github.com/openshift/library-go/pkg/operator/v1helpers" |
| 29 | + |
| 30 | + "github.com/openshift/console-operator/pkg/api" |
| 31 | + "github.com/openshift/console-operator/pkg/console/status" |
| 32 | + oauthsub "github.com/openshift/console-operator/pkg/console/subresource/oauthclient" |
| 33 | + routesub "github.com/openshift/console-operator/pkg/console/subresource/route" |
| 34 | + secretsub "github.com/openshift/console-operator/pkg/console/subresource/secret" |
| 35 | + "github.com/openshift/console-operator/pkg/crypto" |
| 36 | +) |
| 37 | + |
| 38 | +type oauthClientsController struct { |
| 39 | + oauthClient oauthv1client.OAuthClientsGetter |
| 40 | + operatorClient v1helpers.OperatorClient |
| 41 | + secretsClient corev1client.SecretsGetter |
| 42 | + |
| 43 | + oauthClientLister oauthv1lister.OAuthClientLister |
| 44 | + consoleOperatorLister operatorv1listers.ConsoleLister |
| 45 | + routesLister routev1listers.RouteLister |
| 46 | + ingressConfigLister configv1lister.IngressLister |
| 47 | + targetNSSecretsLister corev1listers.SecretLister |
| 48 | + |
| 49 | + statusHandler status.StatusHandler |
| 50 | +} |
| 51 | + |
| 52 | +func NewOAuthClientsController( |
| 53 | + operatorClient v1helpers.OperatorClient, |
| 54 | + oauthClient oauthv1client.OAuthClientsGetter, |
| 55 | + secretsClient corev1client.SecretsGetter, |
| 56 | + oauthClientInformer oauthv1informers.OAuthClientInformer, |
| 57 | + consoleOperatorInformer operatorv1informers.ConsoleInformer, |
| 58 | + routeInformer routev1informers.RouteInformer, |
| 59 | + ingressConfigInformer configv1informers.IngressInformer, |
| 60 | + targetNSsecretsInformer corev1informers.SecretInformer, |
| 61 | + recorder events.Recorder, |
| 62 | +) factory.Controller { |
| 63 | + c := oauthClientsController{ |
| 64 | + oauthClient: oauthClient, |
| 65 | + operatorClient: operatorClient, |
| 66 | + secretsClient: secretsClient, |
| 67 | + |
| 68 | + oauthClientLister: oauthClientInformer.Lister(), |
| 69 | + consoleOperatorLister: consoleOperatorInformer.Lister(), |
| 70 | + routesLister: routeInformer.Lister(), |
| 71 | + ingressConfigLister: ingressConfigInformer.Lister(), |
| 72 | + targetNSSecretsLister: targetNSsecretsInformer.Lister(), |
| 73 | + |
| 74 | + statusHandler: status.NewStatusHandler(operatorClient), |
| 75 | + } |
| 76 | + |
| 77 | + return factory.New(). |
| 78 | + WithSync(c.sync). |
| 79 | + WithInformers( |
| 80 | + consoleOperatorInformer.Informer(), |
| 81 | + routeInformer.Informer(), |
| 82 | + ingressConfigInformer.Informer(), |
| 83 | + targetNSsecretsInformer.Informer(), |
| 84 | + ). |
| 85 | + WithFilteredEventsInformers( |
| 86 | + factory.NamesFilter(api.OAuthClientName), |
| 87 | + oauthClientInformer.Informer(), |
| 88 | + ). |
| 89 | + WithSyncDegradedOnError(operatorClient). |
| 90 | + ToController("OAuthClientsController", recorder.WithComponentSuffix("oauth-clients-controller")) |
| 91 | +} |
| 92 | + |
| 93 | +func (c *oauthClientsController) sync(ctx context.Context, controllerContext factory.SyncContext) error { |
| 94 | + if shouldSync, err := c.handleStatus(ctx); err != nil { |
| 95 | + return err |
| 96 | + } else if !shouldSync { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + operatorConfig, err := c.consoleOperatorLister.Get("cluster") |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + |
| 105 | + ingressConfig, err := c.ingressConfigLister.Get("cluster") |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + routeName := api.OpenShiftConsoleRouteName |
| 111 | + routeConfig := routesub.NewRouteConfig(operatorConfig, ingressConfig, routeName) |
| 112 | + if routeConfig.IsCustomHostnameSet() { |
| 113 | + routeName = api.OpenshiftConsoleCustomRouteName |
| 114 | + } |
| 115 | + |
| 116 | + _, consoleURL, _, routeErr := routesub.GetActiveRouteInfo(c.routesLister, routeName) |
| 117 | + if routeErr != nil { |
| 118 | + return routeErr |
| 119 | + } |
| 120 | + |
| 121 | + clientSecret, secErr := c.syncSecret(ctx, operatorConfig, controllerContext.Recorder()) |
| 122 | + c.statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "FailedApply", secErr)) |
| 123 | + if secErr != nil { |
| 124 | + return c.statusHandler.FlushAndReturn(secErr) |
| 125 | + } |
| 126 | + |
| 127 | + oauthErrReason, oauthErr := c.syncOAuthClient(ctx, clientSecret, consoleURL.String()) |
| 128 | + c.statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSync", oauthErrReason, oauthErr)) |
| 129 | + |
| 130 | + return c.statusHandler.FlushAndReturn(oauthErr) |
| 131 | +} |
| 132 | + |
| 133 | +// handleStatus returns whether sync should happen and any error encountering |
| 134 | +// determining the operator's management state |
| 135 | +// TODO: extract this logic to where it can be used for all controllers |
| 136 | +func (c *oauthClientsController) handleStatus(ctx context.Context) (bool, error) { |
| 137 | + operatorSpec, _, _, err := c.operatorClient.GetOperatorState() |
| 138 | + if err != nil { |
| 139 | + return false, fmt.Errorf("failed to retrieve operator config: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + switch managementState := operatorSpec.ManagementState; managementState { |
| 143 | + case operatorv1.Managed: |
| 144 | + klog.V(4).Infoln("console is in a managed state.") |
| 145 | + return true, nil |
| 146 | + case operatorv1.Unmanaged: |
| 147 | + klog.V(4).Infoln("console is in an unmanaged state.") |
| 148 | + return false, nil |
| 149 | + case operatorv1.Removed: |
| 150 | + klog.V(4).Infoln("console has been removed.") |
| 151 | + return false, c.deregisterClient(ctx) |
| 152 | + default: |
| 153 | + return false, fmt.Errorf("console is in an unknown state: %v", managementState) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func (c *oauthClientsController) syncSecret(ctx context.Context, operatorConfig *operatorv1.Console, recorder events.Recorder) (*corev1.Secret, error) { |
| 158 | + secret, err := c.targetNSSecretsLister.Secrets(api.TargetNamespace).Get(secretsub.Stub().Name) |
| 159 | + if apierrors.IsNotFound(err) || secretsub.GetSecretString(secret) == "" { |
| 160 | + secret, _, err = resourceapply.ApplySecret(ctx, c.secretsClient, recorder, secretsub.DefaultSecret(operatorConfig, crypto.Random256BitsString())) |
| 161 | + } |
| 162 | + // any error should be returned & kill the sync loop |
| 163 | + return secret, err |
| 164 | +} |
| 165 | + |
| 166 | +// applies changes to the oauthclient |
| 167 | +// should not be called until route & secret dependencies are verified |
| 168 | +func (c *oauthClientsController) syncOAuthClient( |
| 169 | + ctx context.Context, |
| 170 | + sec *corev1.Secret, |
| 171 | + consoleURL string, |
| 172 | +) (reason string, err error) { |
| 173 | + oauthClient, err := c.oauthClientLister.Get(oauthsub.Stub().Name) |
| 174 | + if err != nil { |
| 175 | + // at this point we must die & wait for someone to fix the lack of an outhclient. there is nothing we can do. |
| 176 | + return "FailedGet", fmt.Errorf("oauth client for console does not exist and cannot be created (%w)", err) |
| 177 | + } |
| 178 | + clientCopy := oauthClient.DeepCopy() |
| 179 | + oauthsub.RegisterConsoleToOAuthClient(clientCopy, consoleURL, secretsub.GetSecretString(sec)) |
| 180 | + _, _, oauthErr := oauthsub.CustomApplyOAuth(c.oauthClient, clientCopy, ctx) |
| 181 | + if oauthErr != nil { |
| 182 | + return "FailedRegister", oauthErr |
| 183 | + } |
| 184 | + return "", nil |
| 185 | +} |
| 186 | + |
| 187 | +func (c *oauthClientsController) deregisterClient(ctx context.Context) error { |
| 188 | + // existingOAuthClient is not a delete, it is a deregister/neutralize |
| 189 | + existingOAuthClient, err := c.oauthClientLister.Get(oauthsub.Stub().Name) |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + if len(existingOAuthClient.RedirectURIs) == 0 { |
| 195 | + return nil |
| 196 | + } |
| 197 | + |
| 198 | + updated := oauthsub.DeRegisterConsoleFromOAuthClient(existingOAuthClient.DeepCopy()) |
| 199 | + _, err = c.oauthClient.OAuthClients().Update(ctx, updated, metav1.UpdateOptions{}) |
| 200 | + return err |
| 201 | + |
| 202 | +} |
0 commit comments