...

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

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

     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 leaser
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/cluster"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/krmtotf"
    24  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/lease/leasable"
    25  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/servicemapping/servicemappingloader"
    26  
    27  	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
    28  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  )
    31  
    32  type ResourceLeaser struct {
    33  	kubeClient client.Client
    34  	leaser     *Leaser
    35  }
    36  
    37  func NewResourceLeaser(tfProvider *schema.Provider, smLoader *servicemappingloader.ServiceMappingLoader, kubeClient client.Client) *ResourceLeaser {
    38  	return &ResourceLeaser{
    39  		kubeClient: kubeClient,
    40  		leaser:     NewLeaser(tfProvider, smLoader, kubeClient),
    41  	}
    42  }
    43  
    44  // Soft obtain obtains a lease for the resource given the live labels. The method should be called on resources that are leasable.
    45  //
    46  // It does not write the results to GCP so the caller must apply the changes to GCP if persistence is desired
    47  func (l *ResourceLeaser) SoftObtain(ctx context.Context, resource *k8s.Resource, liveLabels map[string]string) error {
    48  	uniqueId, err := cluster.GetNamespaceID(k8s.NamespaceIDConfigMapNN, l.kubeClient, ctx, resource.GetNamespace())
    49  	if err != nil {
    50  		return fmt.Errorf("error getting unique id for namespace '%v': %v", resource.GetNamespace(), err)
    51  	}
    52  	if err := l.leaser.SoftObtain(resource, liveLabels, uniqueId, k8s.TimeToLeaseExpiration, k8s.TimeToLeaseRenewal); err != nil {
    53  		return fmt.Errorf("error obtaining lease: %v", err)
    54  	}
    55  	return nil
    56  }
    57  
    58  func (l *ResourceLeaser) Release(ctx context.Context, u *unstructured.Unstructured) error {
    59  	uniqueId, err := cluster.GetNamespaceID(k8s.NamespaceIDConfigMapNN, l.kubeClient, ctx, u.GetNamespace())
    60  	if err != nil {
    61  		return fmt.Errorf("error getting unique id for namespace '%v': %v", u.GetNamespace(), err)
    62  	}
    63  	if err := l.leaser.Release(ctx, u, uniqueId); err != nil {
    64  		return fmt.Errorf("error releasing lease on %v with name '%v': %v", u.GroupVersionKind(), u.GetName(), err)
    65  	}
    66  	return nil
    67  }
    68  
    69  func (l *ResourceLeaser) IsLeasable(resource *krmtotf.Resource) (ok bool, err error) {
    70  	return leasable.ResourceConfigSupportsLeasing(&resource.ResourceConfig, l.leaser.tfProvider.ResourcesMap)
    71  }
    72  

View as plain text