Documentation
¶
Index ¶
- Constants
- func BuildClusterConfig(info *ClusterInfo, alias string) *clientcmdapi.Config
- func ContextName(clusterID, alias, realm string) string
- func ContextNames(clusterID, alias, realm string, legacyRealms ...string) []string
- func DefaultKubeconfigPath() (string, error)
- func NameRealm(value string, explicit bool) (nameRealm string, legacyRealms []string)
- func OwnedUserNames(info *ClusterInfo) []string
- func UnhashedUserName(info *ClusterInfo) string
- func UserName(info *ClusterInfo) string
- func UserNameForms(info *ClusterInfo) []string
- type ClusterInfo
- type KubeconfigManager
- func (m *KubeconfigManager) GetPath() string
- func (m *KubeconfigManager) ListClusterIDs() ([]string, error)
- func (m *KubeconfigManager) LookupContext(contextName string) (*clientcmdapi.Context, error)
- func (m *KubeconfigManager) RemoveClusterConfig(clusterARN, contextName, userName string, extraUserNames ...string) error
- func (m *KubeconfigManager) RemoveOwnedClusterConfig(clusterID string, contextNames, userNames []string, opts ...RemoveOption) error
- func (m *KubeconfigManager) WriteClusterConfig(info *ClusterInfo, alias, updateMode string) (bool, error)
- type RealmScope
- type RemoveOption
Constants ¶
const ( // ExpectedServerEnv is set by cluster integrations to the API endpoint they provisioned. // Cluster clients use it as a post-condition against the effective kubeconfig target. ExpectedServerEnv = "ATMOS_KUBERNETES_EXPECTED_SERVER" // EndpointGuardEnv enables endpoint enforcement for an explicitly guarded operation. EndpointGuardEnv = "ATMOS_KUBERNETES_ENDPOINT_GUARD" )
Variables ¶
This section is empty.
Functions ¶
func BuildClusterConfig ¶
func BuildClusterConfig(info *ClusterInfo, alias string) *clientcmdapi.Config
BuildClusterConfig creates a kubeconfig api.Config for a single cluster. The exec-plugin command line (ExecArgs/ExecEnv) is supplied by the caller, which keeps this package cloud-agnostic.
func ContextName ¶
ContextName returns the kube context name written for a cluster entry: the alias when one is set, otherwise the cluster ID prefixed with the auth realm (<realm>/<cluster-id>) when the realm is non-empty, otherwise the cluster ID. The realm prefix keeps two repositories with different realms that write the same cluster from sharing, and repointing, one context.
func ContextNames ¶
ContextNames returns ContextName followed by the default context names that older versions wrote for the same entry: the unprefixed cluster ID, when the realm-prefixed default name is in use, and <legacy-realm>/<cluster-id> for each of legacyRealms (see ClusterInfo.LegacyRealms). With an alias, only the alias is returned. Cleanup uses it to also remove contexts written under those older names.
func DefaultKubeconfigPath ¶
DefaultKubeconfigPath returns the XDG-compliant default kubeconfig path. Returns ~/.config/atmos/kube/config.
func NameRealm ¶
NameRealm returns the realm that kube user and default context names include, and the realms whose names still count as the entry's own.
The value is the auth realm and explicit reports whether the user configured it (auth.realm or ATMOS_AUTH_REALM). An explicit realm is returned as nameRealm. A derived realm (Atmos hashes the atmos.yaml path when no realm is configured) must not change names users type, such as `kubectl --context <cluster-arn>`, so nameRealm is empty and the derived value is returned in legacyRealms instead: unreleased builds prefixed names with it, and those entries are cleaned up as this entry's own.
func OwnedUserNames ¶
func OwnedUserNames(info *ClusterInfo) []string
OwnedUserNames returns the exec-plugin user names that belong to the cluster entry described by info: the current UserName, the name without the realm (when a realm is set), the names under each of info.LegacyRealms, and the info.LegacyUserNames older versions wrote. Contexts that point at any other user belong to someone else.
func UnhashedUserName ¶
func UnhashedUserName(info *ClusterInfo) string
UnhashedUserName returns the exec-plugin username without the hash suffix: the form earlier builds wrote for realm- or identity-scoped entries. It is only used to recognize those names as owned for cleanup and migration; new entries are always written under UserName.
func UserName ¶ added in v1.226.0
func UserName(info *ClusterInfo) string
UserName returns the exec-plugin username for a cluster entry: atmos-[<realm>-]<prefix>-<name>-<region>[-<account>][-<identity>]. The realm comes right after "atmos-" so an empty realm keeps the names earlier versions wrote. When the name includes a realm or an identity, a "-<hash>" suffix is appended: the first 8 hex characters of a SHA-256 over every component joined with a NUL byte. The components themselves are joined with "-", which realms, cluster names and identities may contain, so without the hash different tuples could produce the same name (realm "r" with cluster "b-eks-c" versus realm "r-eks-b" with cluster "c") and one entry would overwrite the other's user. Names without a realm or identity are unchanged from earlier versions.
func UserNameForms ¶
func UserNameForms(info *ClusterInfo) []string
UserNameForms returns UserName followed by UnhashedUserName when the two differ, so cleanup recognizes both the current and the earlier form.
Types ¶
type ClusterInfo ¶ added in v1.226.0
type ClusterInfo struct {
// Name is the cluster's short name.
Name string
// Endpoint is the cluster's API server URL.
Endpoint string
// CertificateAuthorityData is the base64-encoded CA certificate, as
// returned by the cloud API. Decoded the same way for every cloud.
CertificateAuthorityData string
// ID uniquely identifies the cluster and is used as the kubeconfig
// cluster map key and default context name: the ARN for EKS, the ARM
// resource ID for AKS, or the canonical GKE resource name. With a
// non-empty Realm, the default context name is <realm>/<ID> (see
// ContextName).
ID string
// Region disambiguates the generated exec-plugin username when the same
// cluster name exists in more than one place: the AWS region for EKS,
// the resource group for AKS, or the project and location for GKE.
Region string
// UserPrefix distinguishes the exec-plugin username by cloud, e.g. "eks"
// or "aks", reproducing the existing "atmos-eks-<name>-<region>" scheme.
UserPrefix string
// AccountID further disambiguates the exec-plugin username for clouds
// where Name+Region is not guaranteed unique: Azure resource groups are
// scoped per-subscription, so the same cluster name and resource group
// can exist in two subscriptions, and EKS clusters of the same name and
// region can exist in two AWS accounts. Leave empty to omit it.
AccountID string
// Identity is the Atmos identity the exec plugin authenticates as. It is
// part of the exec-plugin username so two contexts for the same cluster
// that use different identities never share (and overwrite) one user.
// Leave empty to omit it.
Identity string
// Realm is the Atmos auth realm the entry was written from. A non-empty
// realm is part of the exec-plugin username, so repositories with
// different realms never share (and overwrite) one user in a shared
// kubeconfig, and it is passed to the exec plugin as ATMOS_AUTH_REALM so
// the token is resolved in that realm. It also prefixes the default
// context name (see ContextName). Leave empty to omit all three. Set it
// only for a realm the user configured explicitly (see NameRealm).
Realm string
// LegacyRealms are realms that unreleased builds prefixed this entry's
// user and default context names with although the user never configured
// them: the realm Atmos derives from the atmos.yaml path. Names under
// these realms count as this entry's own (see OwnedUserNames and
// ContextNames), so the next write or cleanup removes them. Leave empty
// when there are none.
LegacyRealms []string
// LegacyUserNames are exec-plugin user names that older versions wrote
// for this entry in formats UserName no longer produces, such as names
// without the account ID or identity. They count as this entry's own
// users (see OwnedUserNames) when an older unprefixed context is
// migrated to the realm-prefixed name. Leave empty when there are none.
LegacyUserNames []string
// ExecArgs are the fully-built kubectl exec-credential-plugin arguments,
// e.g. [aws, eks, token, --cluster-name, X, --region, Y] or
// [azure, aks, token, --cluster-name, X, --resource-group, Y]. Built by
// the caller so this package stays cloud-agnostic.
ExecArgs []string
// ExecEnv are additional environment variables for the exec plugin, e.g.
// ATMOS_IDENTITY.
ExecEnv []clientcmdapi.ExecEnvVar
}
ClusterInfo is the cloud-agnostic cluster data needed to write a kubeconfig entry. Each cloud package (aws, azure) builds one of these from its own describe-cluster call before handing it to KubeconfigManager.
type KubeconfigManager ¶
type KubeconfigManager struct {
// contains filtered or unexported fields
}
KubeconfigManager manages kubeconfig files for Kubernetes clusters (EKS, AKS, GKE).
func NewKubeconfigManager ¶
func NewKubeconfigManager(customPath, modeStr string) (*KubeconfigManager, error)
NewKubeconfigManager creates a manager with the given path and permissions. If customPath is empty, uses XDG default (~/.config/atmos/kube/config). If modeStr is empty, defaults to "0600".
A non-empty customPath is expanded so committable, multi-developer configs and quoted CLI values stay portable: environment variables ($VAR and ${VAR}) are substituted and a leading ~ (or ~/) resolves to the current user's home directory. This covers the integration config path (spec.cluster.kubeconfig.path), the --kubeconfig flag, and the KUBECONFIG/ATMOS_KUBECONFIG env vars alike, since they all flow through here.
func (*KubeconfigManager) GetPath ¶
func (m *KubeconfigManager) GetPath() string
GetPath returns the kubeconfig file path.
func (*KubeconfigManager) ListClusterIDs ¶ added in v1.226.0
func (m *KubeconfigManager) ListClusterIDs() ([]string, error)
ListClusterIDs returns all cluster ARN keys from the kubeconfig file. Returns nil if the file does not exist.
func (*KubeconfigManager) LookupContext ¶
func (m *KubeconfigManager) LookupContext(contextName string) (*clientcmdapi.Context, error)
LookupContext returns the named context from the kubeconfig, or nil when the file or the context does not exist.
func (*KubeconfigManager) RemoveClusterConfig ¶
func (m *KubeconfigManager) RemoveClusterConfig(clusterARN, contextName, userName string, extraUserNames ...string) error
RemoveClusterConfig removes a context from the kubeconfig together with the cluster and users it wrote. Idempotent: returns nil if entries do not exist.
The context is removed only when it points at userName or one of extraUserNames, so cleaning up one integration never removes a context of the same name that another realm, identity or tool pointed at its own user.
The cluster and each user are removed only when no remaining context still references them, so cleaning up one integration never breaks another context that shares the cluster entry (for example, two identities for one cluster) or a user name written by an older version. The extraUserNames argument lists additional users to remove under the same rule, such as a user name in a format that older versions wrote.
func (*KubeconfigManager) RemoveOwnedClusterConfig ¶
func (m *KubeconfigManager) RemoveOwnedClusterConfig(clusterID string, contextNames, userNames []string, opts ...RemoveOption) error
RemoveOwnedClusterConfig removes each of contextNames that points at one of userNames, then the cluster and each of userNames that no remaining context references. A context that points at any other user is left alone, together with its cluster and user. Pass ContextNames as contextNames so a context written under the older, unprefixed default name is cleaned up too. Idempotent: returns nil if entries do not exist. Empty names are ignored.
A context is removed only when it also points at clusterID, so a same-named context of another cluster that shares a user name is never removed. With WithIdentity, a context or user whose exec plugin authenticates as another identity is left alone too.
func (*KubeconfigManager) WriteClusterConfig ¶
func (m *KubeconfigManager) WriteClusterConfig(info *ClusterInfo, alias, updateMode string) (bool, error)
WriteClusterConfig generates and writes kubeconfig for a cluster. Returns changed=true when the on-disk file was modified, and changed=false when the existing kubeconfig already matches the desired state (no write performed). Callers can use this to suppress noisy success messages on repeated invocations that produce identical output.
type RealmScope ¶
type RealmScope struct {
// Realm is the realm included in the user name; empty for no realm.
Realm string
// LegacyRealms are additional realms whose names still count as the entry's own.
LegacyRealms []string
}
RealmScope is the realm a user name is written under and the realms older versions wrote it under (see ClusterInfo.LegacyRealms).
type RemoveOption ¶
type RemoveOption func(*removeOptions)
RemoveOption configures RemoveOwnedClusterConfig.
func WithIdentity ¶
func WithIdentity(identity string) RemoveOption
WithIdentity restricts removal to entries written for identity or for no identity: a context or user whose exec plugin authenticates as a different identity (its ATMOS_IDENTITY env value) is left alone. Entries that name no identity, such as those older versions wrote, still count as owned.