Просмотр исходного кода

Merge pull request #1867 from porter-dev/nafees/porter-apply-new-drivers

[POR-396] 2 new drivers for `porter apply` for env groups and random string
abelanger5 4 лет назад
Родитель
Сommit
fa1fd058a3

+ 21 - 0
api/client/k8s.go

@@ -91,6 +91,27 @@ func (c *Client) GetEnvGroup(
 	return resp, err
 }
 
+func (c *Client) CreateEnvGroup(
+	ctx context.Context,
+	projectID, clusterID uint,
+	namespace string,
+	req *types.CreateEnvGroupRequest,
+) (*types.EnvGroup, error) {
+	resp := &types.EnvGroup{}
+
+	err := c.postRequest(
+		fmt.Sprintf(
+			"/projects/%d/clusters/%d/namespaces/%s/envgroup/create",
+			projectID, clusterID,
+			namespace,
+		),
+		req,
+		resp,
+	)
+
+	return resp, err
+}
+
 func (c *Client) CloneEnvGroup(
 	ctx context.Context,
 	projectID, clusterID uint,

+ 6 - 0
cli/cmd/apply.go

@@ -101,6 +101,8 @@ func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []str
 	worker.RegisterDriver("build-image", preview.NewBuildDriver)
 	worker.RegisterDriver("push-image", preview.NewPushDriver)
 	worker.RegisterDriver("update-config", preview.NewUpdateConfigDriver)
+	worker.RegisterDriver("random-string", preview.NewRandomStringDriver)
+	worker.RegisterDriver("env-group", preview.NewEnvGroupDriver)
 
 	worker.SetDefaultDriver("deploy")
 
@@ -824,6 +826,10 @@ func NewCloneEnvGroupHook(client *api.Client, resourceGroup *switchboardTypes.Re
 
 func (t *CloneEnvGroupHook) PreApply() error {
 	for _, res := range t.resGroup.Resources {
+		if res.Driver == "env-group" {
+			continue
+		}
+
 		config := &ApplicationConfig{}
 
 		err := mapstructure.Decode(res.Config, &config)

+ 105 - 0
cli/cmd/preview/env_group_driver.go

@@ -0,0 +1,105 @@
+package preview
+
+import (
+	"context"
+
+	"github.com/fatih/color"
+	"github.com/mitchellh/mapstructure"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/cli/cmd/config"
+	"github.com/porter-dev/switchboard/pkg/drivers"
+	"github.com/porter-dev/switchboard/pkg/models"
+)
+
+type EnvGroupDriverConfig struct {
+	EnvGroups []*types.EnvGroup `mapstructure:"env_groups"`
+}
+
+type EnvGroupDriver struct {
+	output      map[string]interface{}
+	lookupTable *map[string]drivers.Driver
+	target      *Target
+	config      *EnvGroupDriverConfig
+}
+
+func NewEnvGroupDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
+	driver := &EnvGroupDriver{
+		lookupTable: opts.DriverLookupTable,
+		output:      make(map[string]interface{}),
+	}
+
+	target, err := GetTarget(resource.Target)
+
+	if err != nil {
+		return nil, err
+	}
+
+	driver.target = target
+
+	return driver, nil
+}
+
+func (d *EnvGroupDriver) ShouldApply(resource *models.Resource) bool {
+	return true
+}
+
+func (d *EnvGroupDriver) Apply(resource *models.Resource) (*models.Resource, error) {
+	driverConfig, err := d.getConfig(resource)
+
+	if err != nil {
+		return nil, err
+	}
+
+	d.config = driverConfig
+
+	client := config.GetAPIClient()
+
+	for _, group := range d.config.EnvGroups {
+		if group.Namespace == "" {
+			color.New(color.FgYellow).Printf("env group %s has empty namespace so defaulting to target namespace %s\n",
+				group.Name, d.target.Namespace)
+
+			group.Namespace = d.target.Namespace
+		}
+
+		_, err = client.CreateEnvGroup(
+			context.Background(), d.target.Project, d.target.Cluster, group.Namespace,
+			&types.CreateEnvGroupRequest{
+				Name:      group.Name,
+				Variables: group.Variables,
+			},
+		)
+
+		if err != nil {
+			return nil, err
+		}
+	}
+
+	return resource, nil
+}
+
+func (d *EnvGroupDriver) Output() (map[string]interface{}, error) {
+	return d.output, nil
+}
+
+func (d *EnvGroupDriver) getConfig(resource *models.Resource) (*EnvGroupDriverConfig, error) {
+	populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
+		RawConf:      resource.Config,
+		LookupTable:  *d.lookupTable,
+		Dependencies: resource.Dependencies,
+	})
+
+	if err != nil {
+		return nil, err
+	}
+
+	config := &EnvGroupDriverConfig{}
+
+	err = mapstructure.Decode(populatedConf, config)
+
+	if err != nil {
+		return nil, err
+	}
+
+	return config, nil
+}

+ 67 - 0
cli/cmd/preview/random_string_driver.go

@@ -0,0 +1,67 @@
+package preview
+
+import (
+	"math/rand"
+	"time"
+
+	"github.com/mitchellh/mapstructure"
+	"github.com/porter-dev/switchboard/pkg/drivers"
+	"github.com/porter-dev/switchboard/pkg/models"
+)
+
+const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+
+var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
+
+type RandomStringDriverConfig struct {
+	Length uint
+}
+
+type RandomStringDriver struct {
+	output map[string]interface{}
+	config *RandomStringDriverConfig
+}
+
+func NewRandomStringDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
+	driver := &RandomStringDriver{
+		output: make(map[string]interface{}),
+	}
+
+	driverConfig := &RandomStringDriverConfig{}
+
+	err := mapstructure.Decode(resource.Config, driverConfig)
+
+	if err != nil {
+		return nil, err
+	}
+
+	if driverConfig.Length == 0 {
+		driverConfig.Length = 8
+	}
+
+	driver.config = driverConfig
+
+	return driver, nil
+}
+
+func (d *RandomStringDriver) ShouldApply(resource *models.Resource) bool {
+	return true
+}
+
+func (d *RandomStringDriver) Apply(resource *models.Resource) (*models.Resource, error) {
+	d.output["value"] = randomString(d.config.Length)
+
+	return resource, nil
+}
+
+func (d *RandomStringDriver) Output() (map[string]interface{}, error) {
+	return d.output, nil
+}
+
+func randomString(length uint) string {
+	b := make([]byte, length)
+	for i := range b {
+		b[i] = charset[seededRand.Intn(len(charset))]
+	}
+	return string(b)
+}

+ 0 - 31
cli/cmd/preview/uuid_driver.go

@@ -1,31 +0,0 @@
-package preview
-
-import (
-	"github.com/google/uuid"
-	"github.com/porter-dev/switchboard/pkg/drivers"
-	"github.com/porter-dev/switchboard/pkg/models"
-)
-
-type UUIDDriver struct {
-	output map[string]interface{}
-}
-
-func NewUUIDDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
-	return &BuildDriver{
-		output: make(map[string]interface{}),
-	}, nil
-}
-
-func (d *UUIDDriver) ShouldApply(resource *models.Resource) bool {
-	return true
-}
-
-func (d *UUIDDriver) Apply(resource *models.Resource) (*models.Resource, error) {
-	d.output["uuid"] = uuid.NewString()
-
-	return resource, nil
-}
-
-func (d *UUIDDriver) Output() (map[string]interface{}, error) {
-	return d.output, nil
-}