sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/subnets/subnets.go (about) 1 /* 2 Copyright 2019 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 subnets 18 19 import ( 20 "context" 21 22 asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101" 23 "k8s.io/utils/ptr" 24 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 25 "sigs.k8s.io/cluster-api-provider-azure/azure" 26 "sigs.k8s.io/cluster-api-provider-azure/azure/converters" 27 "sigs.k8s.io/cluster-api-provider-azure/azure/services/aso" 28 "sigs.k8s.io/cluster-api-provider-azure/util/slice" 29 "sigs.k8s.io/controller-runtime/pkg/client" 30 ) 31 32 const serviceName = "subnets" 33 34 // SubnetScope defines the scope interface for a subnet service. 35 type SubnetScope interface { 36 aso.Scope 37 UpdateSubnetID(string, string) 38 UpdateSubnetCIDRs(string, []string) 39 SubnetSpecs() []azure.ASOResourceSpecGetter[*asonetworkv1.VirtualNetworksSubnet] 40 } 41 42 // New creates a new service. 43 func New(scope SubnetScope) *aso.Service[*asonetworkv1.VirtualNetworksSubnet, SubnetScope] { 44 svc := aso.NewService[*asonetworkv1.VirtualNetworksSubnet, SubnetScope](serviceName, scope) 45 svc.ListFunc = list 46 svc.Specs = scope.SubnetSpecs() 47 svc.ConditionType = infrav1.SubnetsReadyCondition 48 svc.PostCreateOrUpdateResourceHook = postCreateOrUpdateResourceHook 49 return svc 50 } 51 52 func postCreateOrUpdateResourceHook(_ context.Context, scope SubnetScope, subnet *asonetworkv1.VirtualNetworksSubnet, err error) error { 53 if err != nil { 54 return err 55 } 56 57 name := subnet.AzureName() 58 scope.UpdateSubnetID(name, ptr.Deref(subnet.Status.Id, "")) 59 scope.UpdateSubnetCIDRs(name, converters.GetSubnetAddresses(*subnet)) 60 61 return nil 62 } 63 64 func list(ctx context.Context, client client.Client, opts ...client.ListOption) ([]*asonetworkv1.VirtualNetworksSubnet, error) { 65 list := &asonetworkv1.VirtualNetworksSubnetList{} 66 err := client.List(ctx, list, opts...) 67 return slice.ToPtrs(list.Items), err 68 }