mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-12 16:09:39 -05:00
Use uid from metadata instead of name
This commit is contained in:
parent
5251041db5
commit
42f273611a
@ -98,8 +98,12 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.With(zap.Error(err)).Fatalf("Failed to marshal PCRs")
|
log.With(zap.Error(err)).Fatalf("Failed to marshal PCRs")
|
||||||
}
|
}
|
||||||
|
cloudControllerManager, err := gcpcloud.NewCloudControllerManager(metadata)
|
||||||
|
if err != nil {
|
||||||
|
log.With(zap.Error(err)).Fatalf("Failed to create cloud controller manager")
|
||||||
|
}
|
||||||
clusterInitJoiner = kubernetes.New(
|
clusterInitJoiner = kubernetes.New(
|
||||||
"gcp", k8sapi.NewKubernetesUtil(), &k8sapi.CoreOSConfiguration{}, kubectl.New(), &gcpcloud.CloudControllerManager{},
|
"gcp", k8sapi.NewKubernetesUtil(), &k8sapi.CoreOSConfiguration{}, kubectl.New(), cloudControllerManager,
|
||||||
&gcpcloud.CloudNodeManager{}, &gcpcloud.Autoscaler{}, metadata, pcrsJSON,
|
&gcpcloud.CloudNodeManager{}, &gcpcloud.Autoscaler{}, metadata, pcrsJSON,
|
||||||
)
|
)
|
||||||
openTPM = vtpm.OpenVTPM
|
openTPM = vtpm.OpenVTPM
|
||||||
|
@ -47,7 +47,7 @@ type CloudControllerManager interface {
|
|||||||
ExtraArgs() []string
|
ExtraArgs() []string
|
||||||
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
||||||
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
||||||
ConfigMaps(instance metadata.InstanceMetadata) (kubernetes.ConfigMaps, error)
|
ConfigMaps() (kubernetes.ConfigMaps, error)
|
||||||
// Secrets returns a list of secrets to deploy together with the k8s cloud-controller-manager.
|
// Secrets returns a list of secrets to deploy together with the k8s cloud-controller-manager.
|
||||||
// Reference: https://kubernetes.io/docs/concepts/configuration/secret/ .
|
// Reference: https://kubernetes.io/docs/concepts/configuration/secret/ .
|
||||||
Secrets(ctx context.Context, providerID, cloudServiceAccountURI string) (kubernetes.Secrets, error)
|
Secrets(ctx context.Context, providerID, cloudServiceAccountURI string) (kubernetes.Secrets, error)
|
||||||
@ -165,7 +165,7 @@ func (m *stubCloudControllerManager) ExtraArgs() []string {
|
|||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *stubCloudControllerManager) ConfigMaps(instance metadata.InstanceMetadata) (kubernetes.ConfigMaps, error) {
|
func (m *stubCloudControllerManager) ConfigMaps() (kubernetes.ConfigMaps, error) {
|
||||||
return []*k8s.ConfigMap{}, nil
|
return []*k8s.ConfigMap{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,7 +339,7 @@ func (k *KubeWrapper) setupCCM(ctx context.Context, subnetworkPodCIDR, cloudServ
|
|||||||
if !k.cloudControllerManager.Supported() {
|
if !k.cloudControllerManager.Supported() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ccmConfigMaps, err := k.cloudControllerManager.ConfigMaps(instance)
|
ccmConfigMaps, err := k.cloudControllerManager.ConfigMaps()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("defining ConfigMaps for CCM: %w", err)
|
return fmt.Errorf("defining ConfigMaps for CCM: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/azureshared"
|
"github.com/edgelesssys/constellation/v2/internal/azureshared"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/versions"
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
||||||
k8s "k8s.io/api/core/v1"
|
k8s "k8s.io/api/core/v1"
|
||||||
@ -61,7 +60,7 @@ func (c *CloudControllerManager) ExtraArgs() []string {
|
|||||||
|
|
||||||
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
||||||
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
||||||
func (c *CloudControllerManager) ConfigMaps(instance metadata.InstanceMetadata) (kubernetes.ConfigMaps, error) {
|
func (c *CloudControllerManager) ConfigMaps() (kubernetes.ConfigMaps, error) {
|
||||||
return kubernetes.ConfigMaps{}, nil
|
return kubernetes.ConfigMaps{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/versions"
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -99,7 +98,7 @@ func TestTrivialCCMFunctions(t *testing.T) {
|
|||||||
assert.NotEmpty(cloud.Path())
|
assert.NotEmpty(cloud.Path())
|
||||||
assert.NotEmpty(cloud.Name())
|
assert.NotEmpty(cloud.Name())
|
||||||
assert.NotEmpty(cloud.ExtraArgs())
|
assert.NotEmpty(cloud.ExtraArgs())
|
||||||
assert.Empty(cloud.ConfigMaps(metadata.InstanceMetadata{}))
|
assert.Empty(cloud.ConfigMaps())
|
||||||
assert.NotEmpty(cloud.Volumes())
|
assert.NotEmpty(cloud.Volumes())
|
||||||
assert.NotEmpty(cloud.VolumeMounts())
|
assert.NotEmpty(cloud.VolumeMounts())
|
||||||
assert.Empty(cloud.Env())
|
assert.Empty(cloud.Env())
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/cloud/metadata"
|
|
||||||
"github.com/edgelesssys/constellation/v2/internal/gcpshared"
|
"github.com/edgelesssys/constellation/v2/internal/gcpshared"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
||||||
"github.com/edgelesssys/constellation/v2/internal/versions"
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
||||||
@ -21,7 +20,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// CloudControllerManager holds the gcp cloud-controller-manager configuration.
|
// CloudControllerManager holds the gcp cloud-controller-manager configuration.
|
||||||
type CloudControllerManager struct{}
|
type CloudControllerManager struct {
|
||||||
|
uid string
|
||||||
|
projectID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCloudControllerManager returns an initialized cloud controller manager configuration struct for GCP.
|
||||||
|
func NewCloudControllerManager(metadata *Metadata) (*CloudControllerManager, error) {
|
||||||
|
uid, err := metadata.api.UID()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getting uid from metadata: %w", err)
|
||||||
|
}
|
||||||
|
projectID, err := metadata.api.RetrieveProjectID()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getting project id from metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &CloudControllerManager{
|
||||||
|
uid: uid,
|
||||||
|
projectID: projectID,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Image returns the container image used to provide cloud-controller-manager for the cloud-provider.
|
// Image returns the container image used to provide cloud-controller-manager for the cloud-provider.
|
||||||
func (c *CloudControllerManager) Image(k8sVersion versions.ValidK8sVersion) (string, error) {
|
func (c *CloudControllerManager) Image(k8sVersion versions.ValidK8sVersion) (string, error) {
|
||||||
@ -52,20 +71,14 @@ func (c *CloudControllerManager) ExtraArgs() []string {
|
|||||||
|
|
||||||
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
||||||
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
||||||
func (c *CloudControllerManager) ConfigMaps(instance metadata.InstanceMetadata) (kubernetes.ConfigMaps, error) {
|
func (c *CloudControllerManager) ConfigMaps() (kubernetes.ConfigMaps, error) {
|
||||||
// GCP CCM expects cloud config to contain the GCP project-id and other configuration.
|
// GCP CCM expects cloud config to contain the GCP project-id and other configuration.
|
||||||
// reference: https://github.com/kubernetes/cloud-provider-gcp/blob/master/cluster/gce/gci/configure-helper.sh#L791-L892
|
// reference: https://github.com/kubernetes/cloud-provider-gcp/blob/master/cluster/gce/gci/configure-helper.sh#L791-L892
|
||||||
var config strings.Builder
|
var config strings.Builder
|
||||||
config.WriteString("[global]\n")
|
config.WriteString("[global]\n")
|
||||||
projectID, _, _, err := gcpshared.SplitProviderID(instance.ProviderID)
|
config.WriteString(fmt.Sprintf("project-id = %s\n", c.projectID))
|
||||||
if err != nil {
|
|
||||||
return kubernetes.ConfigMaps{}, err
|
|
||||||
}
|
|
||||||
config.WriteString(fmt.Sprintf("project-id = %s\n", projectID))
|
|
||||||
config.WriteString("use-metadata-server = true\n")
|
config.WriteString("use-metadata-server = true\n")
|
||||||
|
config.WriteString(fmt.Sprintf("node-tags = constellation-%s\n", c.uid))
|
||||||
nameParts := strings.Split(instance.Name, "-")
|
|
||||||
config.WriteString("node-tags = constellation-" + nameParts[len(nameParts)-2] + "\n")
|
|
||||||
|
|
||||||
return kubernetes.ConfigMaps{
|
return kubernetes.ConfigMaps{
|
||||||
&k8s.ConfigMap{
|
&k8s.ConfigMap{
|
||||||
@ -86,7 +99,7 @@ func (c *CloudControllerManager) ConfigMaps(instance metadata.InstanceMetadata)
|
|||||||
|
|
||||||
// Secrets returns a list of secrets to deploy together with the k8s cloud-controller-manager.
|
// Secrets returns a list of secrets to deploy together with the k8s cloud-controller-manager.
|
||||||
// Reference: https://kubernetes.io/docs/concepts/configuration/secret/ .
|
// Reference: https://kubernetes.io/docs/concepts/configuration/secret/ .
|
||||||
func (c *CloudControllerManager) Secrets(ctx context.Context, _ string, cloudServiceAccountURI string) (kubernetes.Secrets, error) {
|
func (c *CloudControllerManager) Secrets(_ context.Context, _ string, cloudServiceAccountURI string) (kubernetes.Secrets, error) {
|
||||||
serviceAccountKey, err := gcpshared.ServiceAccountKeyFromURI(cloudServiceAccountURI)
|
serviceAccountKey, err := gcpshared.ServiceAccountKeyFromURI(cloudServiceAccountURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return kubernetes.Secrets{}, err
|
return kubernetes.Secrets{}, err
|
||||||
|
@ -28,7 +28,6 @@ func TestConfigMaps(t *testing.T) {
|
|||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
"ConfigMaps works": {
|
"ConfigMaps works": {
|
||||||
instance: metadata.InstanceMetadata{ProviderID: "gce://project-id/zone/instanceName-UID-0", Name: "instanceName-UID-0"},
|
|
||||||
wantConfigMaps: kubernetes.ConfigMaps{
|
wantConfigMaps: kubernetes.ConfigMaps{
|
||||||
&k8s.ConfigMap{
|
&k8s.ConfigMap{
|
||||||
TypeMeta: v1.TypeMeta{
|
TypeMeta: v1.TypeMeta{
|
||||||
@ -49,10 +48,6 @@ node-tags = constellation-UID
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"invalid providerID fails": {
|
|
||||||
instance: metadata.InstanceMetadata{ProviderID: "invalid"},
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tc := range testCases {
|
for name, tc := range testCases {
|
||||||
@ -60,8 +55,11 @@ node-tags = constellation-UID
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
|
|
||||||
cloud := CloudControllerManager{}
|
cloud := CloudControllerManager{
|
||||||
configMaps, err := cloud.ConfigMaps(tc.instance)
|
projectID: "project-id",
|
||||||
|
uid: "UID",
|
||||||
|
}
|
||||||
|
configMaps, err := cloud.ConfigMaps()
|
||||||
|
|
||||||
if tc.wantErr {
|
if tc.wantErr {
|
||||||
assert.Error(err)
|
assert.Error(err)
|
||||||
|
@ -40,7 +40,7 @@ func (c CloudControllerManager) ExtraArgs() []string {
|
|||||||
|
|
||||||
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
||||||
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
||||||
func (c CloudControllerManager) ConfigMaps(instance metadata.InstanceMetadata) (kubernetes.ConfigMaps, error) {
|
func (c CloudControllerManager) ConfigMaps() (kubernetes.ConfigMaps, error) {
|
||||||
return kubernetes.ConfigMaps{}, nil
|
return kubernetes.ConfigMaps{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,13 +10,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var instanceGroupIDRegex = regexp.MustCompile(`^projects/([^/]+)/zones/([^/]+)/instanceGroupManagers/([^/]+)$`)
|
||||||
instanceGroupIDRegex = regexp.MustCompile(`^projects/([^/]+)/zones/([^/]+)/instanceGroupManagers/([^/]+)$`)
|
|
||||||
controlPlaneInstanceGroupNameRegex = regexp.MustCompile(`^(.*)control-plane(.*)$`)
|
|
||||||
workerInstanceGroupNameRegex = regexp.MustCompile(`^(.*)worker(.*)$`)
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *Client) canonicalInstanceGroupID(ctx context.Context, instanceGroupID string) (string, error) {
|
func (c *Client) canonicalInstanceGroupID(ctx context.Context, instanceGroupID string) (string, error) {
|
||||||
project, zone, instanceGroup, err := splitInstanceGroupID(uriNormalize(instanceGroupID))
|
project, zone, instanceGroup, err := splitInstanceGroupID(uriNormalize(instanceGroupID))
|
||||||
@ -41,12 +38,12 @@ func splitInstanceGroupID(instanceGroupID string) (project, zone, instanceGroup
|
|||||||
|
|
||||||
// isControlPlaneInstanceGroup returns true if the instance group is a control plane instance group.
|
// isControlPlaneInstanceGroup returns true if the instance group is a control plane instance group.
|
||||||
func isControlPlaneInstanceGroup(instanceGroupName string) bool {
|
func isControlPlaneInstanceGroup(instanceGroupName string) bool {
|
||||||
return controlPlaneInstanceGroupNameRegex.MatchString(instanceGroupName)
|
return strings.Contains(instanceGroupName, "control-plane")
|
||||||
}
|
}
|
||||||
|
|
||||||
// isWorkerInstanceGroup returns true if the instance group is a worker instance group.
|
// isWorkerInstanceGroup returns true if the instance group is a worker instance group.
|
||||||
func isWorkerInstanceGroup(instanceGroupName string) bool {
|
func isWorkerInstanceGroup(instanceGroupName string) bool {
|
||||||
return workerInstanceGroupNameRegex.MatchString(instanceGroupName)
|
return strings.Contains(instanceGroupName, "worker")
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateInstanceName generates a random instance name.
|
// generateInstanceName generates a random instance name.
|
||||||
|
Loading…
Reference in New Issue
Block a user