sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/natgateways/natgateways.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 natgateways
    18  
    19  import (
    20  	"context"
    21  
    22  	asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20220701"
    23  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    24  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    25  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/aso"
    26  	"sigs.k8s.io/cluster-api-provider-azure/util/slice"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  )
    29  
    30  const serviceName = "natgateways"
    31  
    32  // NatGatewayScope defines the scope interface for NAT gateway service.
    33  type NatGatewayScope interface {
    34  	aso.Scope
    35  	SetNatGatewayIDInSubnets(natGatewayName string, natGatewayID string)
    36  	NatGatewaySpecs() []azure.ASOResourceSpecGetter[*asonetworkv1.NatGateway]
    37  }
    38  
    39  // New creates a new service.
    40  func New(scope NatGatewayScope) *aso.Service[*asonetworkv1.NatGateway, NatGatewayScope] {
    41  	svc := aso.NewService[*asonetworkv1.NatGateway, NatGatewayScope](serviceName, scope)
    42  	svc.ListFunc = list
    43  	svc.Specs = scope.NatGatewaySpecs()
    44  	svc.ConditionType = infrav1.NATGatewaysReadyCondition
    45  	svc.PostCreateOrUpdateResourceHook = postCreateOrUpdateResourceHook
    46  	return svc
    47  }
    48  
    49  func postCreateOrUpdateResourceHook(_ context.Context, scope NatGatewayScope, result *asonetworkv1.NatGateway, err error) error {
    50  	if err != nil {
    51  		return err
    52  	}
    53  	// TODO: ideally we wouldn't need to set the subnet spec based on the result of the create operation
    54  	// result only gets populated when the resource is created or if it already exists
    55  	if result != nil && result.Status.Id != nil {
    56  		scope.SetNatGatewayIDInSubnets(result.Name, *result.Status.Id)
    57  	}
    58  	return nil
    59  }
    60  
    61  func list(ctx context.Context, client client.Client, opts ...client.ListOption) ([]*asonetworkv1.NatGateway, error) {
    62  	list := &asonetworkv1.NatGatewayList{}
    63  	err := client.List(ctx, list, opts...)
    64  	return slice.ToPtrs(list.Items), err
    65  }