@@ -689,6 +689,8 @@ func (r *ResourceSetReconciler) copyResources(ctx context.Context,
689689// convertKubeConfigResources converts kubeconfig data stored in Secrets
690690// into ConfigMap fields by extracting the server and CA certificate.
691691// The conversion is triggered using a specific annotation on the ConfigMap.
692+ // The annotation value must be in the format 'namespace/name' or 'namespace/name:key'.
693+ // When no key is specified, the function looks for 'kubeconfig' first, then 'value'.
692694func (r * ResourceSetReconciler ) convertKubeConfigResources (
693695 ctx context.Context ,
694696 kubeClient client.Client ,
@@ -705,9 +707,20 @@ func (r *ResourceSetReconciler) convertKubeConfigResources(
705707 continue
706708 }
707709
708- sourceParts := strings .Split (source , "/" )
710+ // Parse the annotation value to extract namespace/name and optional key.
711+ // Supported formats: 'namespace/name' or 'namespace/name:key'.
712+ var customKey string
713+ nameRef := source
714+ if colonIdx := strings .LastIndex (source , ":" ); colonIdx > 0 {
715+ if slashIdx := strings .Index (source , "/" ); slashIdx > 0 && colonIdx > slashIdx {
716+ customKey = source [colonIdx + 1 :]
717+ nameRef = source [:colonIdx ]
718+ }
719+ }
720+
721+ sourceParts := strings .Split (nameRef , "/" )
709722 if len (sourceParts ) != 2 {
710- return fmt .Errorf ("invalid %s annotation value '%s' must be in the format 'namespace/name'" , fluxcdv1 .ConvertKubeConfigFromAnnotation , source )
723+ return fmt .Errorf ("invalid %s annotation value '%s' must be in the format 'namespace/name' or 'namespace/name:key' " , fluxcdv1 .ConvertKubeConfigFromAnnotation , source )
711724 }
712725
713726 sourceName := types.NamespacedName {
@@ -717,12 +730,24 @@ func (r *ResourceSetReconciler) convertKubeConfigResources(
717730
718731 secret := & corev1.Secret {}
719732 if err := kubeClient .Get (ctx , sourceName , secret ); err != nil {
720- return fmt .Errorf ("failed to get kubeconfig Secret/%s: %w" , source , err )
733+ return fmt .Errorf ("failed to get kubeconfig Secret/%s: %w" , nameRef , err )
721734 }
722735
723- data , exists := secret .Data ["value" ]
724- if ! exists {
725- return fmt .Errorf ("kubeconfig Secret/%s does not have 'value' field" , source )
736+ var data []byte
737+ var exists bool
738+ if customKey != "" {
739+ data , exists = secret .Data [customKey ]
740+ if ! exists {
741+ return fmt .Errorf ("kubeconfig Secret/%s does not have '%s' field" , nameRef , customKey )
742+ }
743+ } else {
744+ data , exists = secret .Data ["kubeconfig" ]
745+ if ! exists {
746+ data , exists = secret .Data ["value" ]
747+ }
748+ if ! exists {
749+ return fmt .Errorf ("kubeconfig Secret/%s does not have 'kubeconfig' or 'value' field" , nameRef )
750+ }
726751 }
727752
728753 kubeconfigYAML := string (data )
0 commit comments