diff --git a/pkg/clusterconfig/clusterconfig.go b/pkg/clusterconfig/clusterconfig.go index ba0d175099..0d0782838b 100644 --- a/pkg/clusterconfig/clusterconfig.go +++ b/pkg/clusterconfig/clusterconfig.go @@ -1,27 +1,25 @@ package clusterconfig import ( + "context" "fmt" - "strings" "time" - installer "github.com/openshift/installer/pkg/types" - "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" - "k8s.io/apimachinery/pkg/util/yaml" + "k8s.io/client-go/rest" coreset "k8s.io/client-go/kubernetes/typed/core/v1" + configv1 "github.com/openshift/api/config/v1" + osclientset "github.com/openshift/client-go/config/clientset/versioned" imageregistryv1 "github.com/openshift/cluster-image-registry-operator/pkg/apis/imageregistry/v1" regopclient "github.com/openshift/cluster-image-registry-operator/pkg/client" ) const ( - installerConfigNamespace = "kube-system" - installerConfigName = "cluster-config-v1" - cloudCredentialsName = "installer-cloud-credentials" + cloudCredentialsName = "installer-cloud-credentials" ) type StorageType string @@ -68,47 +66,23 @@ func getCoreClient() (*coreset.CoreV1Client, error) { return client, nil } -func GetInstallConfig() (*installer.InstallConfig, error) { - client, err := getCoreClient() - if err != nil { - return nil, err - } - - cm, err := client.ConfigMaps(installerConfigNamespace).Get(installerConfigName, metav1.GetOptions{}) - if err != nil { - return nil, fmt.Errorf("unable to read cluster install configuration: %v", err) - } - - installConfig := &installer.InstallConfig{} - if err := yaml.NewYAMLOrJSONDecoder(strings.NewReader(cm.Data["install-config"]), 100).Decode(installConfig); err != nil { - return nil, fmt.Errorf("unable to decode cluster install configuration: %v", err) - } - - return installConfig, nil -} - -func GetAWSConfig(listers *regopclient.Listers) (*Config, error) { +func GetAWSConfig(ctx context.Context, client rest.Interface, listers *regopclient.Listers) (*Config, error) { cfg := &Config{} - installConfig, err := GetInstallConfig() + infra, err := osclientset.New(client).ConfigV1().Infrastructures().Get("cluster", metav1.GetOptions{}) if err != nil { return nil, err } - if installConfig.Platform.AWS != nil { - cfg.Storage.S3.Region = installConfig.Platform.AWS.Region - } - - client, err := getCoreClient() - if err != nil { - return nil, err + if infra.Status.Platform == configv1.AWSPlatformType { + cfg.Storage.S3.Region = "us-east-1" // FIXME: installConfig.Platform.AWS.Region } // Look for a user defined secret to get the AWS credentials from first sec, err := listers.Secrets.Get(imageregistryv1.ImageRegistryPrivateConfigurationUser) if err != nil && errors.IsNotFound(err) { pollErr := wait.PollImmediate(1*time.Second, 5*time.Minute, func() (stop bool, err error) { - sec, err = client.Secrets(imageregistryv1.ImageRegistryOperatorNamespace).Get(cloudCredentialsName, metav1.GetOptions{}) + sec, err = coreset.New(client).Secrets(imageregistryv1.ImageRegistryOperatorNamespace).Get(cloudCredentialsName, metav1.GetOptions{}) if err != nil { if errors.IsNotFound(err) { return false, nil diff --git a/pkg/storage/s3/s3.go b/pkg/storage/s3/s3.go index 701b83283b..a1a52a3328 100644 --- a/pkg/storage/s3/s3.go +++ b/pkg/storage/s3/s3.go @@ -1,6 +1,7 @@ package s3 import ( + "context" "fmt" "reflect" "strings" @@ -12,11 +13,14 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" + configv1 "github.com/openshift/api/config/v1" operatorapi "github.com/openshift/api/operator/v1" + osclientset "github.com/openshift/client-go/config/clientset/versioned" corev1 "k8s.io/api/core/v1" - + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/uuid" + "k8s.io/client-go/rest" imageregistryv1 "github.com/openshift/cluster-image-registry-operator/pkg/apis/imageregistry/v1" regopclient "github.com/openshift/cluster-image-registry-operator/pkg/client" @@ -46,11 +50,23 @@ func NewDriver(c *imageregistryv1.ImageRegistryConfigStorageS3, listers *regopcl // getS3Service returns a client that allows us to interact // with the aws S3 service func (d *driver) getS3Service() (*s3.S3, error) { + ctx := context.TODO() + if s3Service != nil { return s3Service, nil } - cfg, err := clusterconfig.GetAWSConfig(d.Listers) + config, err := regopclient.GetConfig() + if err != nil { + return nil, err + } + + client, err := rest.RESTClientFor(config) + if err != nil { + return nil, err + } + + cfg, err := clusterconfig.GetAWSConfig(ctx, client, d.Listers) if err != nil { return nil, err } @@ -161,7 +177,19 @@ func (d *driver) Volumes() ([]corev1.Volume, []corev1.VolumeMount, error) { // Secrets returns a map of the storage access secrets. func (d *driver) Secrets() (map[string]string, error) { - cfg, err := clusterconfig.GetAWSConfig(d.Listers) + ctx := context.TODO() + + config, err := regopclient.GetConfig() + if err != nil { + return nil, err + } + + client, err := rest.RESTClientFor(config) + if err != nil { + return nil, err + } + + cfg, err := clusterconfig.GetAWSConfig(ctx, client, d.Listers) if err != nil { return nil, err } @@ -234,12 +262,17 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error { return err } - ic, err := clusterconfig.GetInstallConfig() + config, err := regopclient.GetConfig() + if err != nil { + return err + } + + client, err := rest.RESTClientFor(config) if err != nil { return err } - cv, err := util.GetClusterVersionConfig() + infra, err := osclientset.New(client).ConfigV1().Infrastructures().Get("cluster", metav1.GetOptions{}) if err != nil { return err } @@ -277,7 +310,7 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error { for i := 0; i < 5000; i++ { // If the bucket name is blank, let's generate one if len(d.Config.Bucket) == 0 { - d.Config.Bucket = fmt.Sprintf("%s-%s-%s-%s", imageregistryv1.ImageRegistryName, d.Config.Region, strings.Replace(string(cv.Spec.ClusterID), "-", "", -1), strings.Replace(string(uuid.NewUUID()), "-", "", -1))[0:62] + d.Config.Bucket = fmt.Sprintf("%s-%s-%s-%s", imageregistryv1.ImageRegistryName, d.Config.Region, strings.Replace(infra.Status.InfrastructureName, "-", "", -1), strings.Replace(string(uuid.NewUUID()), "-", "", -1))[0:62] generatedName = true } @@ -326,20 +359,18 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error { return err } - // Tag the bucket with the openshiftClusterID - // along with any user defined tags from the cluster configuration + // Tag the bucket with kubernetes.io/cluster/{infrastructureName} if cr.Status.StorageManaged { - if ic.Platform.AWS != nil { - var tagSet []*s3.Tag - tagSet = append(tagSet, &s3.Tag{Key: aws.String("openshiftClusterID"), Value: aws.String(string(cv.Spec.ClusterID))}) - for k, v := range ic.Platform.AWS.UserTags { - tagSet = append(tagSet, &s3.Tag{Key: aws.String(k), Value: aws.String(v)}) - } - + if infra.Status.Platform == configv1.AWSPlatformType { _, err := svc.PutBucketTagging(&s3.PutBucketTaggingInput{ Bucket: aws.String(d.Config.Bucket), Tagging: &s3.Tagging{ - TagSet: tagSet, + TagSet: []*s3.Tag{ + { + Key: aws.String("kubernetes.io/cluster/" + infra.Status.InfrastructureName), + Value: aws.String("owned"), + }, + }, }, }) if err != nil { @@ -349,7 +380,7 @@ func (d *driver) CreateStorage(cr *imageregistryv1.Config) error { util.UpdateCondition(cr, imageregistryv1.StorageTagged, operatorapi.ConditionFalse, "Unknown Error Occurred", err.Error()) } } else { - util.UpdateCondition(cr, imageregistryv1.StorageTagged, operatorapi.ConditionTrue, "Tagging Successful", "UserTags were successfully applied to the S3 bucket") + util.UpdateCondition(cr, imageregistryv1.StorageTagged, operatorapi.ConditionTrue, "Tagging Successful", "Tags were successfully applied to the S3 bucket") } } } @@ -487,7 +518,19 @@ func (d *driver) RemoveStorage(cr *imageregistryv1.Config) (bool, error) { } func (d *driver) CompleteConfiguration(cr *imageregistryv1.Config) error { - cfg, err := clusterconfig.GetAWSConfig(d.Listers) + ctx := context.TODO() + + config, err := regopclient.GetConfig() + if err != nil { + return err + } + + client, err := rest.RESTClientFor(config) + if err != nil { + return err + } + + cfg, err := clusterconfig.GetAWSConfig(ctx, client, d.Listers) if err != nil { return err } diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 4d2d735b57..6732e357a9 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -4,10 +4,13 @@ import ( "fmt" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/rest" + configv1 "github.com/openshift/api/config/v1" + osclientset "github.com/openshift/client-go/config/clientset/versioned" imageregistryv1 "github.com/openshift/cluster-image-registry-operator/pkg/apis/imageregistry/v1" regopclient "github.com/openshift/cluster-image-registry-operator/pkg/client" - "github.com/openshift/cluster-image-registry-operator/pkg/clusterconfig" "github.com/openshift/cluster-image-registry-operator/pkg/storage/emptydir" "github.com/openshift/cluster-image-registry-operator/pkg/storage/pvc" "github.com/openshift/cluster-image-registry-operator/pkg/storage/s3" @@ -79,17 +82,27 @@ func NewDriver(cfg *imageregistryv1.ImageRegistryConfigStorage, listers *regopcl func getPlatformStorage() (imageregistryv1.ImageRegistryConfigStorage, error) { var cfg imageregistryv1.ImageRegistryConfigStorage - installConfig, err := clusterconfig.GetInstallConfig() + config, err := regopclient.GetConfig() + if err != nil { + return cfg, err + } + + client, err := rest.RESTClientFor(config) + if err != nil { + return cfg, err + } + + infra, err := osclientset.New(client).ConfigV1().Infrastructures().Get("cluster", metav1.GetOptions{}) if err != nil { return cfg, err } switch { - case installConfig.Platform.Libvirt != nil: + case infra.Status.Platform == configv1.LibvirtPlatformType: cfg.EmptyDir = &imageregistryv1.ImageRegistryConfigStorageEmptyDir{} - case installConfig.Platform.AWS != nil: + case infra.Status.Platform == configv1.AWSPlatformType: cfg.S3 = &imageregistryv1.ImageRegistryConfigStorageS3{} - case installConfig.Platform.OpenStack != nil: + case infra.Status.Platform == configv1.OpenStackPlatformType: // TODO(flaper87): This should be switch to swift as soon as support for // it is complete. Using Emptydir for now so that OpenStack deployments // (and work) can move forward for now. Not production ready! diff --git a/pkg/storage/util/util.go b/pkg/storage/util/util.go index e50073bbd8..cb578e9c56 100644 --- a/pkg/storage/util/util.go +++ b/pkg/storage/util/util.go @@ -14,14 +14,9 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metaapi "k8s.io/apimachinery/pkg/apis/meta/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" coreset "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/util/retry" - - configv1 "github.com/openshift/api/config/v1" - - configv1client "github.com/openshift/client-go/config/clientset/versioned" ) // UpdateCondition will update or add the provided condition. @@ -113,16 +108,3 @@ func CreateOrUpdateSecret(name string, namespace string, data map[string]string) return updatedSecret, err } - -func GetClusterVersionConfig() (*configv1.ClusterVersion, error) { - kubeconfig, err := regopclient.GetConfig() - if err != nil { - return nil, err - } - - client, err := configv1client.NewForConfig(kubeconfig) - if err != nil { - return nil, err - } - return client.ConfigV1().ClusterVersions().Get("version", metav1.GetOptions{}) -}