-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for identity service server and updating identity service #1385
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -155,7 +160,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 | ||
|
@@ -166,6 +171,11 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) { | |
return ctrl.Result{}, err | ||
} | ||
|
||
err = s.reconcileIdentityService(ctx, kubeConfig, &log) | ||
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) | ||
|
@@ -447,6 +457,12 @@ 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} | ||
} | ||
|
||
updateClusterRequest := containerpb.UpdateClusterRequest{ | ||
Name: s.scope.ClusterFullName(), | ||
Update: &clusterUpdate, | ||
|
@@ -482,3 +498,70 @@ 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") | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If identity service is not enabled, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, since the server per say does not exist in this scenario. Additionally, I set the string to be optional here https://github.com/kubernetes-sigs/cluster-api-provider-gcp/pull/1385/files#diff-51a950bdfb5cfcf3f2ad6518b8e25bbe6ff584d534f6b9ceeb3aaece9895f30eR177 so it would just not be here. To be clear this is a string, so it would be empty not nil. In case, you were worried about panic. |
||
} | ||
|
||
if kubeConfig == nil { | ||
return "", errors.New("provided kubernetes configuration is nil") | ||
} | ||
|
||
config, err := kubeConfig.ClientConfig() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dynamicClient, err := dynamic.NewForConfig(config) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
resourceID := schema.GroupVersionResource{ | ||
Group: "authentication.gke.io", | ||
Version: "v2alpha1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we parameterize this for simplified maintenance in the future? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming it's subject to change periodically as new API versions are released. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't kubernetes API handle that for us, or are you worried that GKE decide to break the backward compatibility? I am happy to do more if you want, Do you want to loop around versions? or take it as input of the method? |
||
Resource: "clientconfigs", | ||
} | ||
|
||
unstructured, err := dynamicClient.Resource(resourceID).Namespace("kube-public").Get(ctx, "default", metav1.GetOptions{}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
gkeClientConfig := struct { | ||
Spec struct { | ||
Server string `json:"server"` | ||
} `json:"spec"` | ||
}{} | ||
err = runtime.DefaultUnstructuredConverter.FromUnstructured(unstructured.Object, &gkeClientConfig) | ||
|
||
return gkeClientConfig.Spec.Server, err | ||
} |
There was a problem hiding this comment.
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 iserr