|
| 1 | +/* |
| 2 | +Copyright 2026 The Faros Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package agent |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/base64" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/client-go/kubernetes" |
| 29 | + "k8s.io/client-go/rest" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + // inClusterNamespace is the namespace where the agent kubeconfig Secret is stored. |
| 34 | + inClusterNamespace = "kedge-system" |
| 35 | + // kubeconfigSecretKey is the data key within the Secret. |
| 36 | + kubeconfigSecretKey = "kubeconfig" |
| 37 | +) |
| 38 | + |
| 39 | +// IsInCluster returns true when the process is running inside a Kubernetes Pod. |
| 40 | +func IsInCluster() bool { |
| 41 | + return os.Getenv("KUBERNETES_SERVICE_HOST") != "" |
| 42 | +} |
| 43 | + |
| 44 | +// AgentKubeconfigSecretName returns the name of the Secret used to persist |
| 45 | +// the hub kubeconfig for the given edge when running in-cluster. |
| 46 | +func AgentKubeconfigSecretName(edgeName string) string { |
| 47 | + return "kedge-agent-" + edgeName + "-kubeconfig" |
| 48 | +} |
| 49 | + |
| 50 | +// newInClusterKubernetesClient builds a Kubernetes clientset using in-cluster |
| 51 | +// service-account credentials. |
| 52 | +func newInClusterKubernetesClient() (*kubernetes.Clientset, error) { |
| 53 | + cfg, err := rest.InClusterConfig() |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("building in-cluster config: %w", err) |
| 56 | + } |
| 57 | + cs, err := kubernetes.NewForConfig(cfg) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("creating in-cluster kubernetes client: %w", err) |
| 60 | + } |
| 61 | + return cs, nil |
| 62 | +} |
| 63 | + |
| 64 | +// decodeKubeconfigB64 decodes a base64-encoded kubeconfig string as returned |
| 65 | +// by the hub's token-exchange header. |
| 66 | +func decodeKubeconfigB64(kubeconfigB64 string) (string, error) { |
| 67 | + b, err := base64.StdEncoding.DecodeString(kubeconfigB64) |
| 68 | + if err != nil { |
| 69 | + return "", fmt.Errorf("decoding base64 kubeconfig: %w", err) |
| 70 | + } |
| 71 | + return string(b), nil |
| 72 | +} |
| 73 | + |
| 74 | +// SaveKubeconfigToSecret writes the hub kubeconfig to the in-cluster Secret so |
| 75 | +// that it survives a pod restart. |
| 76 | +func SaveKubeconfigToSecret(edgeName, kubeconfigData string) error { |
| 77 | + cs, err := newInClusterKubernetesClient() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + secretName := AgentKubeconfigSecretName(edgeName) |
| 82 | + existing, err := cs.CoreV1().Secrets(inClusterNamespace).Get( |
| 83 | + context.Background(), |
| 84 | + secretName, |
| 85 | + metav1.GetOptions{}, |
| 86 | + ) |
| 87 | + if apierrors.IsNotFound(err) { |
| 88 | + _, err = cs.CoreV1().Secrets(inClusterNamespace).Create( |
| 89 | + context.Background(), |
| 90 | + &corev1.Secret{ |
| 91 | + ObjectMeta: metav1.ObjectMeta{ |
| 92 | + Name: secretName, |
| 93 | + Namespace: inClusterNamespace, |
| 94 | + }, |
| 95 | + Type: corev1.SecretTypeOpaque, |
| 96 | + Data: map[string][]byte{ |
| 97 | + kubeconfigSecretKey: []byte(kubeconfigData), |
| 98 | + }, |
| 99 | + }, |
| 100 | + metav1.CreateOptions{}, |
| 101 | + ) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("creating kubeconfig secret: %w", err) |
| 104 | + } |
| 105 | + return nil |
| 106 | + } |
| 107 | + if err != nil { |
| 108 | + return fmt.Errorf("getting kubeconfig secret: %w", err) |
| 109 | + } |
| 110 | + if existing.Data == nil { |
| 111 | + existing.Data = make(map[string][]byte) |
| 112 | + } |
| 113 | + existing.Data[kubeconfigSecretKey] = []byte(kubeconfigData) |
| 114 | + _, err = cs.CoreV1().Secrets(inClusterNamespace).Update( |
| 115 | + context.Background(), |
| 116 | + existing, |
| 117 | + metav1.UpdateOptions{}, |
| 118 | + ) |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("updating kubeconfig secret: %w", err) |
| 121 | + } |
| 122 | + return nil |
| 123 | +} |
0 commit comments