mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-06-19 11:44:20 -04:00
kubernetes: move k8s-components creation to internal
The CLI will have to create similar objects for k8s upgrades.
This commit is contained in:
parent
7db584a88e
commit
3cebd68c24
2 changed files with 44 additions and 27 deletions
|
@ -27,6 +27,7 @@ import (
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/gcpshared"
|
"github.com/edgelesssys/constellation/v2/internal/cloud/gcpshared"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/constants"
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/deploy/helm"
|
"github.com/edgelesssys/constellation/v2/internal/deploy/helm"
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/logger"
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/role"
|
"github.com/edgelesssys/constellation/v2/internal/role"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
||||||
|
@ -305,34 +306,16 @@ func (k *KubeWrapper) JoinCluster(ctx context.Context, args *kubeadm.BootstrapTo
|
||||||
// setupK8sComponentsConfigMap applies a ConfigMap (cf. server-side apply) to store the installed k8s components.
|
// setupK8sComponentsConfigMap applies a ConfigMap (cf. server-side apply) to store the installed k8s components.
|
||||||
// It returns the name of the ConfigMap.
|
// It returns the name of the ConfigMap.
|
||||||
func (k *KubeWrapper) setupK8sComponentsConfigMap(ctx context.Context, components components.Components, clusterVersion string) (string, error) {
|
func (k *KubeWrapper) setupK8sComponentsConfigMap(ctx context.Context, components components.Components, clusterVersion string) (string, error) {
|
||||||
componentsMarshalled, err := json.Marshal(components)
|
componentsConfig, err := kubernetes.ConstructK8sComponentsCM(components, clusterVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("marshalling component versions: %w", err)
|
return "", fmt.Errorf("constructing k8s-components ConfigMap: %w", err)
|
||||||
}
|
|
||||||
componentsHash := components.GetHash()
|
|
||||||
componentConfigMapName := fmt.Sprintf("k8s-components-%s", strings.ReplaceAll(componentsHash, ":", "-"))
|
|
||||||
|
|
||||||
componentsConfig := corev1.ConfigMap{
|
|
||||||
TypeMeta: metav1.TypeMeta{
|
|
||||||
APIVersion: "v1",
|
|
||||||
Kind: "ConfigMap",
|
|
||||||
},
|
|
||||||
Immutable: toPtr(true),
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: componentConfigMapName,
|
|
||||||
Namespace: "kube-system",
|
|
||||||
},
|
|
||||||
Data: map[string]string{
|
|
||||||
constants.ComponentsListKey: string(componentsMarshalled),
|
|
||||||
constants.K8sVersionFieldName: clusterVersion,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := k.client.CreateConfigMap(ctx, componentsConfig); err != nil {
|
if err := k.client.CreateConfigMap(ctx, componentsConfig); err != nil {
|
||||||
return "", fmt.Errorf("apply in KubeWrapper.setupK8sVersionConfigMap(..) for components config map failed with: %w", err)
|
return "", fmt.Errorf("apply in KubeWrapper.setupK8sVersionConfigMap(..) for components config map failed with: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return componentConfigMapName, nil
|
return componentsConfig.ObjectMeta.Name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// setupInternalConfigMap applies a ConfigMap (cf. server-side apply) to store information that is not supposed to be user-editable.
|
// setupInternalConfigMap applies a ConfigMap (cf. server-side apply) to store information that is not supposed to be user-editable.
|
||||||
|
@ -516,7 +499,3 @@ type constellationServicesConfig struct {
|
||||||
cloudServiceAccountURI string
|
cloudServiceAccountURI string
|
||||||
loadBalancerIP string
|
loadBalancerIP string
|
||||||
}
|
}
|
||||||
|
|
||||||
func toPtr[T any](v T) *T {
|
|
||||||
return &v
|
|
||||||
}
|
|
||||||
|
|
|
@ -7,12 +7,19 @@ SPDX-License-Identifier: AGPL-3.0-only
|
||||||
package kubernetes
|
package kubernetes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
k8s "k8s.io/api/core/v1"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
||||||
|
"github.com/edgelesssys/constellation/v2/internal/versions/components"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigMaps represent a list of k8s ConfigMap.
|
// ConfigMaps represent a list of k8s ConfigMap.
|
||||||
type ConfigMaps []*k8s.ConfigMap
|
type ConfigMaps []*corev1.ConfigMap
|
||||||
|
|
||||||
// Marshal marshals config maps into multiple YAML documents.
|
// Marshal marshals config maps into multiple YAML documents.
|
||||||
func (s ConfigMaps) Marshal() ([]byte, error) {
|
func (s ConfigMaps) Marshal() ([]byte, error) {
|
||||||
|
@ -22,3 +29,34 @@ func (s ConfigMaps) Marshal() ([]byte, error) {
|
||||||
}
|
}
|
||||||
return MarshalK8SResourcesList(objects)
|
return MarshalK8SResourcesList(objects)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConstructK8sComponentsCM creates a k8s-components config map for the given components.
|
||||||
|
func ConstructK8sComponentsCM(components components.Components, clusterVersion string) (corev1.ConfigMap, error) {
|
||||||
|
componentsMarshalled, err := json.Marshal(components)
|
||||||
|
if err != nil {
|
||||||
|
return corev1.ConfigMap{}, fmt.Errorf("marshalling component versions: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
componentsHash := components.GetHash()
|
||||||
|
componentConfigMapName := fmt.Sprintf("k8s-components-%s", strings.ReplaceAll(componentsHash, ":", "-"))
|
||||||
|
|
||||||
|
return corev1.ConfigMap{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
APIVersion: "v1",
|
||||||
|
Kind: "ConfigMap",
|
||||||
|
},
|
||||||
|
Immutable: toPtr(true),
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: componentConfigMapName,
|
||||||
|
Namespace: "kube-system",
|
||||||
|
},
|
||||||
|
Data: map[string]string{
|
||||||
|
constants.ComponentsListKey: string(componentsMarshalled),
|
||||||
|
constants.K8sVersionFieldName: clusterVersion,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func toPtr[T any](v T) *T {
|
||||||
|
return &v
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue