|
| 1 | +/* |
| 2 | +Copyright 2024 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 scope |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + |
| 26 | + "github.com/IBM/go-sdk-core/v5/core" |
| 27 | + "github.com/IBM/platform-services-go-sdk/resourcecontrollerv2" |
| 28 | + |
| 29 | + "k8s.io/klog/v2/textlogger" |
| 30 | + |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + |
| 33 | + capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 34 | + "sigs.k8s.io/cluster-api/util/patch" |
| 35 | + |
| 36 | + infrav1beta2 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2" |
| 37 | + "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/authenticator" |
| 38 | + "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos" |
| 39 | + "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller" |
| 40 | + "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager" |
| 41 | + "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc" |
| 42 | + "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/endpoints" |
| 43 | +) |
| 44 | + |
| 45 | +const ( |
| 46 | + // LOGDEBUGLEVEL indicates the debug level of the logs. |
| 47 | + LOGDEBUGLEVEL = 5 |
| 48 | +) |
| 49 | + |
| 50 | +// VPCClusterScopeParams defines the input parameters used to create a new VPCClusterScope. |
| 51 | +type VPCClusterScopeParams struct { |
| 52 | + Client client.Client |
| 53 | + Cluster *capiv1beta1.Cluster |
| 54 | + IBMVPCCluster *infrav1beta2.IBMVPCCluster |
| 55 | + Logger logr.Logger |
| 56 | + ServiceEndpoint []endpoints.ServiceEndpoint |
| 57 | + |
| 58 | + IBMVPCClient vpc.Vpc |
| 59 | +} |
| 60 | + |
| 61 | +// VPCClusterScope defines a scope defined around a VPC Cluster. |
| 62 | +type VPCClusterScope struct { |
| 63 | + logr.Logger |
| 64 | + Client client.Client |
| 65 | + patchHelper *patch.Helper |
| 66 | + |
| 67 | + COSClient cos.Cos |
| 68 | + ResourceControllerClient resourcecontroller.ResourceController |
| 69 | + ResourceManagerClient resourcemanager.ResourceManager |
| 70 | + VPCClient vpc.Vpc |
| 71 | + |
| 72 | + Cluster *capiv1beta1.Cluster |
| 73 | + IBMVPCCluster *infrav1beta2.IBMVPCCluster |
| 74 | + ServiceEndpoint []endpoints.ServiceEndpoint |
| 75 | +} |
| 76 | + |
| 77 | +// NewVPCClusterScope creates a new VPCClusterScope from the supplied parameters. |
| 78 | +func NewVPCClusterScope(params VPCClusterScopeParams) (*VPCClusterScope, error) { |
| 79 | + if params.Client == nil { |
| 80 | + err := errors.New("error failed to generate new scope from nil Client") |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + if params.Cluster == nil { |
| 84 | + err := errors.New("error failed to generate new scope from nil Cluster") |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + if params.IBMVPCCluster == nil { |
| 88 | + err := errors.New("error failed to generate new scope from nil IBMVPCCluster") |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + if params.Logger == (logr.Logger{}) { |
| 92 | + params.Logger = textlogger.NewLogger(textlogger.NewConfig()) |
| 93 | + } |
| 94 | + |
| 95 | + helper, err := patch.NewHelper(params.IBMVPCCluster, params.Client) |
| 96 | + if err != nil { |
| 97 | + return nil, fmt.Errorf("error failed to init patch helper: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + vpcEndpoint := endpoints.FetchVPCEndpoint(params.IBMVPCCluster.Spec.Region, params.ServiceEndpoint) |
| 101 | + vpcClient, err := vpc.NewService(vpcEndpoint) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("error failed to create IBM VPC client: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + if params.IBMVPCCluster.Spec.Network == nil || params.IBMVPCCluster.Spec.Region == "" { |
| 107 | + return nil, fmt.Errorf("error failed to generate vpc client as Network or Region is nil") |
| 108 | + } |
| 109 | + |
| 110 | + if params.Logger.V(LOGDEBUGLEVEL).Enabled() { |
| 111 | + core.SetLoggingLevel(core.LevelDebug) |
| 112 | + } |
| 113 | + |
| 114 | + auth, err := authenticator.GetAuthenticator() |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("error failed to create authenticator: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Create Global Tagging client. |
| 120 | + // TODO(cjschaef): need service support. |
| 121 | + |
| 122 | + // Create Resource Controller client. |
| 123 | + rcOptions := resourcecontroller.ServiceOptions{ |
| 124 | + ResourceControllerV2Options: &resourcecontrollerv2.ResourceControllerV2Options{ |
| 125 | + Authenticator: auth, |
| 126 | + }, |
| 127 | + } |
| 128 | + // Fetch the resource controller endpoint. |
| 129 | + rcEndpoint := endpoints.FetchEndpoints(string(endpoints.RC), params.ServiceEndpoint) |
| 130 | + if rcEndpoint != "" { |
| 131 | + rcOptions.URL = rcEndpoint |
| 132 | + params.Logger.V(3).Info("Overriding the default resource controller endpoint", "ResourceControllerEndpoint", rcEndpoint) |
| 133 | + } |
| 134 | + resourceControllerClient, err := resourcecontroller.NewService(rcOptions) |
| 135 | + if err != nil { |
| 136 | + return nil, fmt.Errorf("error failed to create resource controller client: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + // Create Resource Manager client. |
| 140 | + // TODO(cjschaef): Need to extend ResourceManager service and endpoint support to add properly. |
| 141 | + |
| 142 | + clusterScope := &VPCClusterScope{ |
| 143 | + Logger: params.Logger, |
| 144 | + Client: params.Client, |
| 145 | + patchHelper: helper, |
| 146 | + Cluster: params.Cluster, |
| 147 | + IBMVPCCluster: params.IBMVPCCluster, |
| 148 | + ServiceEndpoint: params.ServiceEndpoint, |
| 149 | + ResourceControllerClient: resourceControllerClient, |
| 150 | + VPCClient: vpcClient, |
| 151 | + } |
| 152 | + return clusterScope, nil |
| 153 | +} |
| 154 | + |
| 155 | +// PatchObject persists the cluster configuration and status. |
| 156 | +func (s *VPCClusterScope) PatchObject() error { |
| 157 | + return s.patchHelper.Patch(context.TODO(), s.IBMVPCCluster) |
| 158 | +} |
| 159 | + |
| 160 | +// Close closes the current scope persisting the cluster configuration and status. |
| 161 | +func (s *VPCClusterScope) Close() error { |
| 162 | + return s.PatchObject() |
| 163 | +} |
| 164 | + |
| 165 | +// Name returns the CAPI cluster name. |
| 166 | +func (s *VPCClusterScope) Name() string { |
| 167 | + return s.Cluster.Name |
| 168 | +} |
0 commit comments