|
| 1 | +package clusters |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + flag "github.com/spf13/pflag" |
| 7 | + "k8s.io/apimachinery/pkg/runtime" |
| 8 | + "k8s.io/client-go/rest" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/cluster" |
| 11 | + |
| 12 | + "github.com/openmcp-project/controller-utils/pkg/controller" |
| 13 | +) |
| 14 | + |
| 15 | +type Cluster struct { |
| 16 | + // identifier (for logging purposes only) |
| 17 | + id string |
| 18 | + // path to kubeconfig |
| 19 | + cfgPath string |
| 20 | + // cluster config |
| 21 | + restCfg *rest.Config |
| 22 | + // client |
| 23 | + client client.Client |
| 24 | + // cluster |
| 25 | + cluster cluster.Cluster |
| 26 | +} |
| 27 | + |
| 28 | +// Initializes a new cluster. |
| 29 | +// Panics if id is empty. |
| 30 | +func New(id string) *Cluster { |
| 31 | + c := &Cluster{} |
| 32 | + c.InitializeID(id) |
| 33 | + return c |
| 34 | +} |
| 35 | + |
| 36 | +// WithConfigPath sets the config path for the cluster. |
| 37 | +// Returns the cluster for chaining. |
| 38 | +func (c *Cluster) WithConfigPath(cfgPath string) *Cluster { |
| 39 | + c.cfgPath = cfgPath |
| 40 | + return c |
| 41 | +} |
| 42 | + |
| 43 | +// RegisterConfigPathFlag adds a flag '--<id>-cluster' for the cluster's config path to the given flag set. |
| 44 | +// Panics if the cluster's id is not set. |
| 45 | +func (c *Cluster) RegisterConfigPathFlag(flags *flag.FlagSet) { |
| 46 | + if !c.HasID() { |
| 47 | + panic("cluster id must be set before registering the config path flag") |
| 48 | + } |
| 49 | + flags.StringVar(&c.cfgPath, fmt.Sprintf("%s-cluster", c.id), "", fmt.Sprintf("Path to the %s cluster kubeconfig file or directory containing either a kubeconfig or host, token, and ca file. Leave empty to use in-cluster config.", c.id)) |
| 50 | +} |
| 51 | + |
| 52 | +/////////////////// |
| 53 | +// STATUS CHECKS // |
| 54 | +/////////////////// |
| 55 | + |
| 56 | +// HasID returns true if the cluster has an id. |
| 57 | +// If this returns false, initialize a new cluster via New() or InitializeID(). |
| 58 | +func (c *Cluster) HasID() bool { |
| 59 | + return c != nil && c.id != "" |
| 60 | +} |
| 61 | + |
| 62 | +// HasRESTConfig returns true if the cluster has a REST config. |
| 63 | +// If this returns false, load the config via InitializeRESTConfig(). |
| 64 | +func (c *Cluster) HasRESTConfig() bool { |
| 65 | + return c != nil && c.restCfg != nil |
| 66 | +} |
| 67 | + |
| 68 | +// HasClient returns true if the cluster has a client. |
| 69 | +// If this returns false, create a client via InitializeClient(). |
| 70 | +func (c *Cluster) HasClient() bool { |
| 71 | + return c != nil && c.client != nil |
| 72 | +} |
| 73 | + |
| 74 | +////////////////// |
| 75 | +// INITIALIZERS // |
| 76 | +////////////////// |
| 77 | + |
| 78 | +// InitializeID sets the cluster's id. |
| 79 | +// Panics if id is empty. |
| 80 | +func (c *Cluster) InitializeID(id string) { |
| 81 | + if id == "" { |
| 82 | + panic("id must not be empty") |
| 83 | + } |
| 84 | + c.id = id |
| 85 | +} |
| 86 | + |
| 87 | +// InitializeRESTConfig loads the cluster's REST config. |
| 88 | +// If the config has already been loaded, this is a no-op. |
| 89 | +// Panics if the cluster's id is not set (InitializeID must be called first). |
| 90 | +func (c *Cluster) InitializeRESTConfig() error { |
| 91 | + if !c.HasID() { |
| 92 | + panic("cluster id must be set before loading the config") |
| 93 | + } |
| 94 | + if c.HasRESTConfig() { |
| 95 | + return nil |
| 96 | + } |
| 97 | + cfg, err := controller.LoadKubeconfig(c.cfgPath) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to load '%s' cluster kubeconfig: %w", c.ID(), err) |
| 100 | + } |
| 101 | + c.restCfg = cfg |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// InitializeClient creates a new client for the cluster. |
| 106 | +// This also initializes the cluster's controller-runtime 'Cluster' representation. |
| 107 | +// If the client has already been initialized, this is a no-op. |
| 108 | +// Panics if the cluster's REST config has not been loaded (InitializeRESTConfig must be called first). |
| 109 | +func (c *Cluster) InitializeClient(scheme *runtime.Scheme) error { |
| 110 | + if !c.HasRESTConfig() { |
| 111 | + panic("cluster REST config must be set before creating the client") |
| 112 | + } |
| 113 | + if c.HasClient() { |
| 114 | + return nil |
| 115 | + } |
| 116 | + cli, err := client.New(c.restCfg, client.Options{Scheme: scheme}) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("failed to create '%s' cluster client: %w", c.ID(), err) |
| 119 | + } |
| 120 | + clu, err := cluster.New(c.restCfg, func(o *cluster.Options) { o.Scheme = scheme }) |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("failed to create '%s' cluster Cluster representation: %w", c.ID(), err) |
| 123 | + } |
| 124 | + c.client = cli |
| 125 | + c.cluster = clu |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +///////////// |
| 130 | +// GETTERS // |
| 131 | +///////////// |
| 132 | + |
| 133 | +// ID returns the cluster's id. |
| 134 | +func (c *Cluster) ID() string { |
| 135 | + return c.id |
| 136 | +} |
| 137 | + |
| 138 | +// ConfigPath returns the cluster's config path. |
| 139 | +func (c *Cluster) ConfigPath() string { |
| 140 | + return c.cfgPath |
| 141 | +} |
| 142 | + |
| 143 | +// RESTConfig returns the cluster's REST config. |
| 144 | +// This returns a pointer, but modification can lead to inconsistent behavior and is not recommended. |
| 145 | +func (c *Cluster) RESTConfig() *rest.Config { |
| 146 | + return c.restCfg |
| 147 | +} |
| 148 | + |
| 149 | +// Client returns the cluster's client. |
| 150 | +func (c *Cluster) Client() client.Client { |
| 151 | + return c.client |
| 152 | +} |
| 153 | + |
| 154 | +// Cluster returns the cluster's controller-runtime 'Cluster' representation. |
| 155 | +func (c *Cluster) Cluster() cluster.Cluster { |
| 156 | + return c.cluster |
| 157 | +} |
| 158 | + |
| 159 | +// Scheme returns the cluster's scheme. |
| 160 | +// Returns nil if the client has not been initialized. |
| 161 | +func (c *Cluster) Scheme() *runtime.Scheme { |
| 162 | + if c.cluster == nil { |
| 163 | + return nil |
| 164 | + } |
| 165 | + return c.cluster.GetScheme() |
| 166 | +} |
| 167 | + |
| 168 | +// APIServerEndpoint returns the cluster's API server endpoint. |
| 169 | +// Returns an empty string if the REST config has not been initialized. |
| 170 | +func (c *Cluster) APIServerEndpoint() string { |
| 171 | + if c.restCfg == nil { |
| 172 | + return "" |
| 173 | + } |
| 174 | + return c.restCfg.Host |
| 175 | +} |
0 commit comments