...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/lease/leasable/leasable.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/lease/leasable

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package leasable
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/extension"
    22  	tfresource "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/tf/resource"
    23  
    24  	tfschema "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
    25  	"github.com/nasa9084/go-openapi"
    26  )
    27  
    28  func ResourceConfigSupportsLeasing(rc *v1alpha1.ResourceConfig, tfResourceMap map[string]*tfschema.Resource) (ok bool, err error) {
    29  	// Disable leasing for DataflowJob since the leasing mechanism needs to
    30  	// modify the resource's labels regularly, which, for DataflowJob, would
    31  	// trigger a job update on each modification.
    32  	if rc.Kind == "DataflowJob" {
    33  		return false, nil
    34  	}
    35  	labelsField := rc.MetadataMapping.Labels
    36  	if labelsField == "" {
    37  		return false, nil
    38  	}
    39  	tfResource, ok := tfResourceMap[rc.Name]
    40  	if !ok {
    41  		return false, fmt.Errorf("unknown resource %v", rc.Name)
    42  	}
    43  	labelsFieldSchema, err := tfresource.GetTFSchemaForField(tfResource, labelsField)
    44  	if err != nil {
    45  		return false, fmt.Errorf("error getting schema for field '%v' of resource '%v': %v", labelsField, rc.Name, err)
    46  	}
    47  	labelsFieldIsMutable := !labelsFieldSchema.ForceNew
    48  	return labelsFieldIsMutable, nil
    49  }
    50  
    51  func DCLSchemaSupportsLeasing(schema *openapi.Schema) (bool, error) {
    52  	labelsField, s, found, err := extension.GetLabelsFieldSchema(schema)
    53  	if err != nil {
    54  		return false, fmt.Errorf("error getting schema for field %v of resource '%v': %w", labelsField, schema.Title, err)
    55  	}
    56  	if !found {
    57  		return false, nil
    58  	}
    59  	isImmutable, err := extension.IsImmutableField(s)
    60  	if err != nil {
    61  		return false, err
    62  	}
    63  	return !isImmutable, nil
    64  }
    65  

View as plain text