2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package gcp
|
|
|
|
|
|
|
|
import (
|
2022-05-24 04:04:42 -04:00
|
|
|
"context"
|
2022-03-25 05:55:37 -04:00
|
|
|
"encoding/json"
|
2022-03-22 11:03:15 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/gcpshared"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kubernetes"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/versions"
|
2022-03-25 05:55:37 -04:00
|
|
|
k8s "k8s.io/api/core/v1"
|
|
|
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// CloudControllerManager holds the gcp cloud-controller-manager configuration.
|
2022-09-26 08:11:24 -04:00
|
|
|
type CloudControllerManager struct {
|
|
|
|
uid string
|
|
|
|
projectID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCloudControllerManager returns an initialized cloud controller manager configuration struct for GCP.
|
2022-10-24 10:58:21 -04:00
|
|
|
func NewCloudControllerManager(ctx context.Context, metadata *Metadata) (*CloudControllerManager, error) {
|
|
|
|
uid, err := metadata.api.UID(ctx)
|
2022-09-26 08:11:24 -04:00
|
|
|
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
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
// Image returns the container image used to provide cloud-controller-manager for the cloud-provider.
|
2022-07-22 09:05:04 -04:00
|
|
|
func (c *CloudControllerManager) Image(k8sVersion versions.ValidK8sVersion) (string, error) {
|
|
|
|
return versions.VersionConfigs[k8sVersion].CloudControllerManagerImageGCP, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the path used by cloud-controller-manager executable within the container image.
|
|
|
|
func (c *CloudControllerManager) Path() string {
|
|
|
|
return "/cloud-controller-manager"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the cloud-provider name as used by k8s cloud-controller-manager (k8s.gcr.io/cloud-controller-manager).
|
|
|
|
func (c *CloudControllerManager) Name() string {
|
|
|
|
return "gce"
|
|
|
|
}
|
|
|
|
|
2022-03-25 05:55:37 -04:00
|
|
|
// ExtraArgs returns a list of arguments to append to the cloud-controller-manager command.
|
|
|
|
func (c *CloudControllerManager) ExtraArgs() []string {
|
|
|
|
return []string{
|
|
|
|
"--use-service-account-credentials",
|
2022-05-24 04:04:42 -04:00
|
|
|
"--controllers=cloud-node,cloud-node-lifecycle,nodeipam,service,route",
|
2022-03-25 05:55:37 -04:00
|
|
|
"--cloud-config=/etc/gce/gce.conf",
|
2022-05-24 04:04:42 -04:00
|
|
|
"--cidr-allocator-type=CloudAllocator",
|
|
|
|
"--allocate-node-cidrs=true",
|
|
|
|
"--configure-cloud-routes=false",
|
2022-03-25 05:55:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigMaps returns a list of ConfigMaps to deploy together with the k8s cloud-controller-manager
|
|
|
|
// Reference: https://kubernetes.io/docs/concepts/configuration/configmap/ .
|
2022-09-26 08:11:24 -04:00
|
|
|
func (c *CloudControllerManager) ConfigMaps() (kubernetes.ConfigMaps, error) {
|
2022-03-25 05:55:37 -04:00
|
|
|
// GCP CCM expects cloud config to contain the GCP project-id and other configuration.
|
2022-03-22 11:03:15 -04:00
|
|
|
// reference: https://github.com/kubernetes/cloud-provider-gcp/blob/master/cluster/gce/gci/configure-helper.sh#L791-L892
|
|
|
|
var config strings.Builder
|
|
|
|
config.WriteString("[global]\n")
|
2022-09-26 08:11:24 -04:00
|
|
|
config.WriteString(fmt.Sprintf("project-id = %s\n", c.projectID))
|
2022-05-24 04:04:42 -04:00
|
|
|
config.WriteString("use-metadata-server = true\n")
|
2022-09-26 08:11:24 -04:00
|
|
|
config.WriteString(fmt.Sprintf("node-tags = constellation-%s\n", c.uid))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.ConfigMaps{
|
2022-03-25 05:55:37 -04:00
|
|
|
&k8s.ConfigMap{
|
|
|
|
TypeMeta: v1.TypeMeta{
|
|
|
|
Kind: "ConfigMap",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "gceconf",
|
|
|
|
Namespace: "kube-system",
|
|
|
|
},
|
|
|
|
Data: map[string]string{
|
|
|
|
"gce.conf": config.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Secrets returns a list of secrets to deploy together with the k8s cloud-controller-manager.
|
|
|
|
// Reference: https://kubernetes.io/docs/concepts/configuration/secret/ .
|
2022-09-26 08:11:24 -04:00
|
|
|
func (c *CloudControllerManager) Secrets(_ context.Context, _ string, cloudServiceAccountURI string) (kubernetes.Secrets, error) {
|
2022-06-07 08:52:06 -04:00
|
|
|
serviceAccountKey, err := gcpshared.ServiceAccountKeyFromURI(cloudServiceAccountURI)
|
2022-03-25 05:55:37 -04:00
|
|
|
if err != nil {
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.Secrets{}, err
|
2022-03-25 05:55:37 -04:00
|
|
|
}
|
|
|
|
rawKey, err := json.Marshal(serviceAccountKey)
|
|
|
|
if err != nil {
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.Secrets{}, err
|
2022-03-25 05:55:37 -04:00
|
|
|
}
|
|
|
|
|
2022-08-29 08:30:20 -04:00
|
|
|
return kubernetes.Secrets{
|
2022-03-25 05:55:37 -04:00
|
|
|
&k8s.Secret{
|
|
|
|
TypeMeta: v1.TypeMeta{
|
|
|
|
Kind: "Secret",
|
|
|
|
APIVersion: "v1",
|
|
|
|
},
|
|
|
|
ObjectMeta: v1.ObjectMeta{
|
|
|
|
Name: "gcekey",
|
|
|
|
Namespace: "kube-system",
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
|
|
|
"key.json": rawKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Volumes returns a list of volumes to deploy together with the k8s cloud-controller-manager.
|
|
|
|
// Reference: https://kubernetes.io/docs/concepts/storage/volumes/ .
|
|
|
|
func (c *CloudControllerManager) Volumes() []k8s.Volume {
|
|
|
|
return []k8s.Volume{
|
|
|
|
{
|
|
|
|
Name: "gceconf",
|
|
|
|
VolumeSource: k8s.VolumeSource{
|
|
|
|
ConfigMap: &k8s.ConfigMapVolumeSource{
|
|
|
|
LocalObjectReference: k8s.LocalObjectReference{
|
|
|
|
Name: "gceconf",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "gcekey",
|
|
|
|
VolumeSource: k8s.VolumeSource{
|
|
|
|
Secret: &k8s.SecretVolumeSource{
|
|
|
|
SecretName: "gcekey",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VolumeMounts returns a list of volume mounts to deploy together with the k8s cloud-controller-manager.
|
|
|
|
func (c *CloudControllerManager) VolumeMounts() []k8s.VolumeMount {
|
|
|
|
return []k8s.VolumeMount{
|
|
|
|
{
|
|
|
|
Name: "gceconf",
|
|
|
|
ReadOnly: true,
|
|
|
|
MountPath: "/etc/gce",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "gcekey",
|
|
|
|
ReadOnly: true,
|
|
|
|
MountPath: "/var/secrets/google",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Env returns a list of k8s environment key-value pairs to deploy together with the k8s cloud-controller-manager.
|
|
|
|
func (c *CloudControllerManager) Env() []k8s.EnvVar {
|
|
|
|
return []k8s.EnvVar{
|
|
|
|
{
|
|
|
|
Name: "GOOGLE_APPLICATION_CREDENTIALS",
|
|
|
|
Value: "/var/secrets/google/key.json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// Supported is used to determine if cloud controller manager is implemented for this cloud provider.
|
|
|
|
func (c *CloudControllerManager) Supported() bool {
|
|
|
|
return true
|
|
|
|
}
|