Skip to content
Open
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
1 change: 1 addition & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ WORKDIR /
COPY --from=tilt-helper /process.txt .
COPY --from=tilt-helper /start.sh .
COPY --from=tilt-helper /restart.sh .
COPY --from=tilt-helper /process.txt .
COPY --from=tilt-helper /go/bin/dlv .
COPY manager .
"""
Expand Down
43 changes: 22 additions & 21 deletions cloud/services/container/clusters/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,35 @@ const (
GkeScope = "https://www.googleapis.com/auth/cloud-platform"
)

func (s *Service) reconcileKubeconfig(ctx context.Context, cluster *containerpb.Cluster, log *logr.Logger) error {
func (s *Service) reconcileKubeconfig(ctx context.Context, cluster *containerpb.Cluster, log *logr.Logger) (clientcmd.ClientConfig, error) {
log.Info("Reconciling kubeconfig")
clusterRef := types.NamespacedName{
Name: s.scope.Cluster.Name,
Namespace: s.scope.Cluster.Namespace,
}
var kubeConfig *api.Config

configSecret, err := secret.GetFromNamespacedName(ctx, s.scope.Client(), clusterRef, secret.Kubeconfig)
if err != nil {
if !apierrors.IsNotFound(err) {
log.Error(err, "getting kubeconfig secret", "name", clusterRef)
return fmt.Errorf("getting kubeconfig secret %s: %w", clusterRef, err)
return nil, fmt.Errorf("getting kubeconfig secret %s: %w", clusterRef, err)
}
log.Info("kubeconfig secret not found, creating")

if createErr := s.createCAPIKubeconfigSecret(
if kubeConfig, err = s.createCAPIKubeconfigSecret(
ctx,
cluster,
&clusterRef,
log,
); createErr != nil {
return fmt.Errorf("creating kubeconfig secret: %w", createErr)
); err != nil {
return nil, fmt.Errorf("creating kubeconfig secret: %w", err)
}
} else if updateErr := s.updateCAPIKubeconfigSecret(ctx, configSecret); updateErr != nil {
return fmt.Errorf("updating kubeconfig secret: %w", err)
} else if kubeConfig, err = s.updateCAPIKubeconfigSecret(ctx, configSecret); err != nil {
return nil, fmt.Errorf("updating kubeconfig secret: %w", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this also fix the error it should have been updateErr, now everything is err

}

return nil
return clientcmd.NewDefaultClientConfig(*kubeConfig, nil), nil
}

func (s *Service) reconcileAdditionalKubeconfigs(ctx context.Context, cluster *containerpb.Cluster, log *logr.Logger) error {
Expand Down Expand Up @@ -133,21 +134,21 @@ func (s *Service) createUserKubeconfigSecret(ctx context.Context, cluster *conta
return nil
}

func (s *Service) createCAPIKubeconfigSecret(ctx context.Context, cluster *containerpb.Cluster, clusterRef *types.NamespacedName, log *logr.Logger) error {
func (s *Service) createCAPIKubeconfigSecret(ctx context.Context, cluster *containerpb.Cluster, clusterRef *types.NamespacedName, log *logr.Logger) (*api.Config, error) {
controllerOwnerRef := *metav1.NewControllerRef(s.scope.GCPManagedControlPlane, infrav1exp.GroupVersion.WithKind("GCPManagedControlPlane"))

contextName := s.getKubeConfigContextName(false)

cfg, err := s.createBaseKubeConfig(contextName, cluster)
if err != nil {
log.Error(err, "failed creating base config")
return fmt.Errorf("creating base kubeconfig: %w", err)
return nil, fmt.Errorf("creating base kubeconfig: %w", err)
}

token, err := s.generateToken(ctx)
if err != nil {
log.Error(err, "failed generating token")
return err
return nil, err
}
cfg.AuthInfos = map[string]*api.AuthInfo{
contextName: {
Expand All @@ -158,50 +159,50 @@ func (s *Service) createCAPIKubeconfigSecret(ctx context.Context, cluster *conta
out, err := clientcmd.Write(*cfg)
if err != nil {
log.Error(err, "failed serializing kubeconfig to yaml")
return fmt.Errorf("serialize kubeconfig to yaml: %w", err)
return nil, fmt.Errorf("serialize kubeconfig to yaml: %w", err)
}

kubeconfigSecret := kubeconfig.GenerateSecretWithOwner(*clusterRef, out, controllerOwnerRef)
if err := s.scope.Client().Create(ctx, kubeconfigSecret); err != nil {
log.Error(err, "failed creating secret")
return fmt.Errorf("creating secret: %w", err)
return nil, fmt.Errorf("creating secret: %w", err)
}

return nil
return cfg, nil
}

func (s *Service) updateCAPIKubeconfigSecret(ctx context.Context, configSecret *corev1.Secret) error {
func (s *Service) updateCAPIKubeconfigSecret(ctx context.Context, configSecret *corev1.Secret) (*api.Config, error) {
data, ok := configSecret.Data[secret.KubeconfigDataName]
if !ok {
return errors.Errorf("missing key %q in secret data", secret.KubeconfigDataName)
return nil, errors.Errorf("missing key %q in secret data", secret.KubeconfigDataName)
}

config, err := clientcmd.Load(data)
if err != nil {
return errors.Wrap(err, "failed to convert kubeconfig Secret into a clientcmdapi.Config")
return nil, errors.Wrap(err, "failed to convert kubeconfig Secret into a clientcmdapi.Config")
}

token, err := s.generateToken(ctx)
if err != nil {
return err
return nil, err
}

contextName := s.getKubeConfigContextName(false)
config.AuthInfos[contextName].Token = token

out, err := clientcmd.Write(*config)
if err != nil {
return errors.Wrap(err, "failed to serialize config to yaml")
return nil, errors.Wrap(err, "failed to serialize config to yaml")
}

configSecret.Data[secret.KubeconfigDataName] = out

err = s.scope.Client().Update(ctx, configSecret)
if err != nil {
return fmt.Errorf("updating kubeconfig secret: %w", err)
return nil, fmt.Errorf("updating kubeconfig secret: %w", err)
}

return nil
return config, nil
}

func (s *Service) getKubeConfigContextName(isUser bool) string {
Expand Down
94 changes: 93 additions & 1 deletion cloud/services/container/clusters/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import (
"github.com/googleapis/gax-go/v2/apierror"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
infrav1exp "sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/util/reconciler"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -157,7 +162,7 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
conditions.MarkFalse(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneUpdatingCondition, infrav1exp.GKEControlPlaneUpdatedReason, clusterv1.ConditionSeverityInfo, "")

// Reconcile kubeconfig
err = s.reconcileKubeconfig(ctx, cluster, &log)
kubeConfig, err := s.reconcileKubeconfig(ctx, cluster, &log)
if err != nil {
log.Error(err, "Failed to reconcile CAPI kubeconfig")
return ctrl.Result{}, err
Expand All @@ -168,6 +173,11 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
return ctrl.Result{}, err
}

err = s.reconcileIdentityService(ctx, kubeConfig, &log)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I always recommend the one-line form when we don't need the error later:

if err := s.reconcileIdentityService(..); err != nil {

(No need to fix, just my 2c)

if err != nil {
return ctrl.Result{}, err
}

s.scope.SetEndpoint(cluster.GetEndpoint())
conditions.MarkTrue(s.scope.ConditionSetter(), clusterv1.ReadyCondition)
conditions.MarkTrue(s.scope.ConditionSetter(), infrav1exp.GKEControlPlaneReadyCondition)
Expand Down Expand Up @@ -481,6 +491,13 @@ func (s *Service) checkDiffAndPrepareUpdate(existingCluster *containerpb.Cluster
log.V(4).Info("Master authorized networks config update check", "desired", desiredMasterAuthorizedNetworksConfig)
}

desiredEnableIdentityService := s.scope.GCPManagedControlPlane.Spec.EnableIdentityService
if desiredEnableIdentityService != existingCluster.GetIdentityServiceConfig().GetEnabled() {
needUpdate = true
clusterUpdate.DesiredIdentityServiceConfig = &containerpb.IdentityServiceConfig{Enabled: desiredEnableIdentityService}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I believe that some (OK, most) fields cannot be updated in "one shot". https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1/ClusterUpdate says "Exactly one update can be applied to a cluster with each request, so at most one field can be provided."

I think the easiest way to handle this is probably to build the UpdateClusterRequest as we are doing here, but then to break it down into one-field-at-a-time requests when we actually go to call UpdateCluster

(I don't know whether we want to handle in this PR - or maybe it is handled somewhere else and I missed it - but it is a classic gotcha that I'm sure we'll hit!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting limit I did not know about. We're actually updating the UpdateClusterRequest with all of the changes we detect and then calling UpdateCluster(). Should we be having issues with GCP rejecting multiple changes at the same time? I don't recall seeing this. Does it mean that only one of the updated fields is applied and an update of multiple fields needs as many reconciliations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can update to:

# Update alone, only one update at a time is supported 
if !needUpdate && desiredEnableIdentityService != existingCluster.GetIdentityServiceConfig().GetEnabled() {

That should be done everywhere after. does that look better?

log.V(2).Info("Identity service config update required", "current", existingCluster.GetIdentityServiceConfig().GetEnabled(), "desired", desiredEnableIdentityService)
}

updateClusterRequest := containerpb.UpdateClusterRequest{
Name: s.scope.ClusterFullName(),
Update: &clusterUpdate,
Expand Down Expand Up @@ -516,3 +533,78 @@ func compareMasterAuthorizedNetworksConfig(a, b *containerpb.MasterAuthorizedNet
}
return true
}

// reconcileIdentityService set the identity service server in the status of the GCPManagedControlPlane.
func (s *Service) reconcileIdentityService(ctx context.Context, kubeConfig clientcmd.ClientConfig, log *logr.Logger) error {
identityServiceServer, err := s.getIdentityServiceServer(ctx, kubeConfig)
if err != nil {
err = fmt.Errorf("failed to retrieve identity service: %w", err)
log.Error(err, "Failed to retrieve identity service server")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I personally think we shouldn't do this, we should rely on the caller logging, but I'm guessing this happens more often than we would like

(Another thought, likely not for this PR - we should decide whether we should pass the logr in, vs getting it from the ctx)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second this, it is cleaner to have the caller logging the error and the called method only return the error here.

I also agree on the logger being passed as an argument, which we're doing already, and I think is overly convoluted, but I suggest we discuss this in a separate issue and open a stand-alone PR to tidy it up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I follow the current patterns, I am happy to change. I am not 100% sure what we want to remove.
Should I just return?

		return err

return err
}

s.scope.GCPManagedControlPlane.Status.IdentityServiceServer = identityServiceServer

return nil
}

// getIdentityServiceServer retrieve the server to use for authentication using the identity service.
func (s *Service) getIdentityServiceServer(ctx context.Context, kubeConfig clientcmd.ClientConfig) (string, error) {
/*
# Example of the ClientConfig (see https://cloud.google.com/kubernetes-engine/docs/how-to/oidc#configuring_on_a_cluster):
apiVersion: authentication.gke.io/v2alpha1
kind: ClientConfig
metadata:
name: default
namespace: kube-public
spec:
server: https://192.168.0.1:6443
*/

if !s.scope.GCPManagedControlPlane.Spec.EnableIdentityService {
// Identity service is not enabled, skipping
return "", nil
}

if kubeConfig == nil {
return "", errors.New("provided kubernetes configuration is nil")
}

config, err := kubeConfig.ClientConfig()
if err != nil {
return "", fmt.Errorf("failed to get client config: %w", err)
}

dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return "", fmt.Errorf("failed to create dynamic client: %w", err)
}

resourceID := schema.GroupVersionResource{
Group: "authentication.gke.io",
Version: "v2alpha1",
Resource: "clientconfigs",
}

unstructured, err := dynamicClient.Resource(resourceID).Namespace("kube-public").Get(ctx, "default", metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("failed to get identity service client config: %w", err)
}

gkeClientConfig := struct {
Spec struct {
Server string `json:"server"`
} `json:"spec"`
}{}

err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured.Object, &gkeClientConfig)
if err != nil {
return "", fmt.Errorf("failed to convert unstructured to client config: %w", err)
}

if gkeClientConfig.Spec.Server == "" {
return "", errors.New("identity service server URL is empty")
}

return gkeClientConfig.Spec.Server, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ spec:

Deprecated: This field will soon be removed and you are expected to use Version instead.
type: string
identityServiceServer:
description: IdentityServiceServer indicates when the identity service
is enabled, the server for external authentication.
type: string
initialized:
description: |-
Initialized is true when the control plane is available for initial contact.
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/developers/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ It will setup the network, if you already setup the network you can skip this st
```

By default, the Cluster API components deployed by Tilt have experimental features turned off.
If you would like to enable these features, add `extra_args` as specified in [The Cluster API Book](https://cluster-api.sigs.k8s.io/developer/tilt.html#create-a-tilt-settingsjson-file).
If you would like to enable these features, add `extra_args` as specified in [The Cluster API Book](https://cluster-api.sigs.k8s.io/developer/core/tilt.html?highlight=tilt#create-a-tilt-settings-file).

Once your kind management cluster is up and running, you can [deploy a workload cluster](#deploying-a-workload-cluster).

Expand Down
4 changes: 4 additions & 0 deletions exp/api/v1beta1/gcpmanagedcontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ type GCPManagedControlPlaneStatus struct {
// Version represents the version of the GKE control plane.
// +optional
Version *string `json:"version,omitempty"`

// IdentityServiceServer indicates when the identity service is enabled, the server for external authentication.
// +optional
IdentityServiceServer string `json:"identityServiceServer,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down