github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/kubernetes/provider/resources/interface.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resources
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	corev1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/client-go/kubernetes"
    13  
    14  	"github.com/juju/juju/core/status"
    15  )
    16  
    17  //go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/resources_mock.go github.com/juju/juju/caas/kubernetes/provider/resources Resource,Applier
    18  
    19  const (
    20  	// JujuFieldManager marks the resource changes were made by Juju.
    21  	JujuFieldManager = "juju"
    22  )
    23  
    24  // Resource defines methods for manipulating a k8s resource.
    25  type Resource interface {
    26  	metav1.ObjectMetaAccessor
    27  	// Clone returns a copy of the resource.
    28  	Clone() Resource
    29  	// Apply patches the resource change.
    30  	Apply(ctx context.Context, client kubernetes.Interface) error
    31  	// Get refreshes the resource.
    32  	Get(ctx context.Context, client kubernetes.Interface) error
    33  	// Delete removes the resource.
    34  	Delete(ctx context.Context, client kubernetes.Interface) error
    35  	// String returns a string format containing the name and type of the resource.
    36  	String() string
    37  	// ComputeStatus returns a juju status for the resource.
    38  	ComputeStatus(ctx context.Context, client kubernetes.Interface, now time.Time) (string, status.Status, time.Time, error)
    39  	// Events emitted by the object.
    40  	Events(ctx context.Context, client kubernetes.Interface) ([]corev1.Event, error)
    41  	// ID returns a comparable ID for the Resource
    42  	ID() ID
    43  }
    44  
    45  // Applier defines methods for processing a slice of resource operations.
    46  type Applier interface {
    47  	// Apply adds apply operations to the applier.
    48  	Apply(...Resource)
    49  	// Delete adds delete operations to the applier.
    50  	Delete(...Resource)
    51  	// ApplySet deletes Resources in the current slice that don't exist in the
    52  	// desired slice. All items in the desired slice are applied.
    53  	ApplySet(current []Resource, desired []Resource)
    54  	// Run processes the slice of the operations.
    55  	Run(ctx context.Context, client kubernetes.Interface, noRollback bool) error
    56  }
    57  
    58  // ID represents a compareable identifier for Resources.
    59  type ID struct {
    60  	Type      string
    61  	Name      string
    62  	Namespace string
    63  }