sigs.k8s.io/cluster-api@v1.7.1/internal/controllers/topology/cluster/patches/external/external_patch_generator.go (about)

     1  /*
     2  Copyright 2022 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 external implements the external patch generator.
    18  package external
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/pkg/errors"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    27  	runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
    28  	"sigs.k8s.io/cluster-api/feature"
    29  	"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches/api"
    30  	runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
    31  )
    32  
    33  // externalPatchGenerator generates JSON patches for a GeneratePatchesRequest based on a ClusterClassPatch.
    34  type externalPatchGenerator struct {
    35  	runtimeClient runtimeclient.Client
    36  	patch         *clusterv1.ClusterClassPatch
    37  }
    38  
    39  // NewGenerator returns a new external Generator from a given ClusterClassPatch object.
    40  func NewGenerator(runtimeClient runtimeclient.Client, patch *clusterv1.ClusterClassPatch) api.Generator {
    41  	return &externalPatchGenerator{
    42  		runtimeClient: runtimeClient,
    43  		patch:         patch,
    44  	}
    45  }
    46  
    47  func (e externalPatchGenerator) Generate(ctx context.Context, forObject client.Object, req *runtimehooksv1.GeneratePatchesRequest) (*runtimehooksv1.GeneratePatchesResponse, error) {
    48  	if !feature.Gates.Enabled(feature.RuntimeSDK) {
    49  		return nil, errors.Errorf("can not use external patch %q if RuntimeSDK feature flag is disabled", *e.patch.External.GenerateExtension)
    50  	}
    51  
    52  	// Set the settings defined in external patch definition on the request object.
    53  	// These settings will override overlapping keys defined in ExtensionConfig settings.
    54  	req.Settings = e.patch.External.Settings
    55  	// The req object is re-used across multiple calls to the patch generator.
    56  	// Reset the settings so that the request does not remain modified here.
    57  	defer func() {
    58  		req.Settings = nil
    59  	}()
    60  
    61  	resp := &runtimehooksv1.GeneratePatchesResponse{}
    62  	err := e.runtimeClient.CallExtension(ctx, runtimehooksv1.GeneratePatches, forObject, *e.patch.External.GenerateExtension, req, resp)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return resp, nil
    67  }