sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/aksextensions/spec.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package aksextensions
    18  
    19  import (
    20  	"context"
    21  
    22  	asokubernetesconfigurationv1 "github.com/Azure/azure-service-operator/v2/api/kubernetesconfiguration/v1api20230501"
    23  	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/utils/ptr"
    26  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    27  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    28  )
    29  
    30  // AKSExtensionSpec defines the specification for an AKS Extension.
    31  type AKSExtensionSpec struct {
    32  	Name                    string
    33  	Namespace               string
    34  	AKSAssignedIdentityType infrav1.AKSAssignedIdentity
    35  	ExtensionIdentity       infrav1.ExtensionIdentity
    36  	AutoUpgradeMinorVersion *bool
    37  	ConfigurationSettings   map[string]string
    38  	ExtensionType           *string
    39  	ReleaseTrain            *string
    40  	Version                 *string
    41  	Owner                   string
    42  	OwnerRef                metav1.OwnerReference
    43  	Plan                    *infrav1.ExtensionPlan
    44  	Scope                   infrav1.ExtensionScope
    45  }
    46  
    47  // ResourceRef implements azure.ASOResourceSpecGetter.
    48  func (s *AKSExtensionSpec) ResourceRef() *asokubernetesconfigurationv1.Extension {
    49  	return &asokubernetesconfigurationv1.Extension{
    50  		ObjectMeta: metav1.ObjectMeta{
    51  			Name:      azure.GetNormalizedKubernetesName(s.Name),
    52  			Namespace: s.Namespace,
    53  		},
    54  	}
    55  }
    56  
    57  // Parameters implements azure.ASOResourceSpecGetter.
    58  func (s *AKSExtensionSpec) Parameters(ctx context.Context, existingAKSExtension *asokubernetesconfigurationv1.Extension) (parameters *asokubernetesconfigurationv1.Extension, err error) {
    59  	aksExtension := &asokubernetesconfigurationv1.Extension{}
    60  	if existingAKSExtension != nil {
    61  		aksExtension = existingAKSExtension
    62  	}
    63  
    64  	aksExtension.Spec = asokubernetesconfigurationv1.Extension_Spec{}
    65  	aksExtension.Spec.AzureName = s.Name
    66  	aksExtension.Spec.AutoUpgradeMinorVersion = s.AutoUpgradeMinorVersion
    67  	aksExtension.Spec.ConfigurationSettings = s.ConfigurationSettings
    68  	aksExtension.Spec.ExtensionType = s.ExtensionType
    69  	aksExtension.Spec.ReleaseTrain = s.ReleaseTrain
    70  	aksExtension.Spec.Version = s.Version
    71  	aksExtension.Spec.Owner = &genruntime.ArbitraryOwnerReference{
    72  		ARMID: s.Owner,
    73  	}
    74  
    75  	if s.Plan != nil {
    76  		aksExtension.Spec.Plan = &asokubernetesconfigurationv1.Plan{
    77  			Name:      ptr.To(s.Plan.Name),
    78  			Product:   ptr.To(s.Plan.Product),
    79  			Publisher: ptr.To(s.Plan.Publisher),
    80  			Version:   ptr.To(s.Plan.Version),
    81  		}
    82  	}
    83  	if s.ExtensionIdentity != "" {
    84  		aksExtension.Spec.Identity = &asokubernetesconfigurationv1.Identity{
    85  			Type: (*asokubernetesconfigurationv1.Identity_Type)(ptr.To(s.ExtensionIdentity)),
    86  		}
    87  	}
    88  	if s.AKSAssignedIdentityType != "" {
    89  		aksExtension.Spec.AksAssignedIdentity = &asokubernetesconfigurationv1.Extension_Properties_AksAssignedIdentity_Spec{
    90  			Type: (*asokubernetesconfigurationv1.Extension_Properties_AksAssignedIdentity_Type_Spec)(ptr.To(s.AKSAssignedIdentityType)),
    91  		}
    92  	}
    93  	if s.Scope.ScopeType == infrav1.ExtensionScopeCluster {
    94  		aksExtension.Spec.Scope = &asokubernetesconfigurationv1.Scope{
    95  			Cluster: &asokubernetesconfigurationv1.ScopeCluster{
    96  				ReleaseNamespace: ptr.To(s.Scope.ReleaseNamespace),
    97  			},
    98  		}
    99  	} else if s.Scope.ScopeType == infrav1.ExtensionScopeNamespace {
   100  		aksExtension.Spec.Scope = &asokubernetesconfigurationv1.Scope{
   101  			Namespace: &asokubernetesconfigurationv1.ScopeNamespace{
   102  				TargetNamespace: ptr.To(s.Scope.TargetNamespace),
   103  			},
   104  		}
   105  	}
   106  
   107  	return aksExtension, nil
   108  }
   109  
   110  // WasManaged implements azure.ASOResourceSpecGetter.
   111  func (s *AKSExtensionSpec) WasManaged(resource *asokubernetesconfigurationv1.Extension) bool {
   112  	// returns always returns true as CAPZ does not support BYO extension.
   113  	return true
   114  }