Skip to content

Commit

Permalink
create and update service deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Sep 27, 2023
1 parent d6cb195 commit 34f3397
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 6 deletions.
178 changes: 175 additions & 3 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package plural

import (
"fmt"
"strings"

gqlclient "github.com/pluralsh/console-client-go"
"github.com/pluralsh/plural/pkg/utils"
"github.com/urfave/cli"
"k8s.io/apimachinery/pkg/util/yaml"
)

func init() {
Expand Down Expand Up @@ -64,6 +68,40 @@ func (p *Plural) cdServiceCommands() []cli.Command {
Action: latestVersion(requireArgs(p.handleListClusterServices, []string{"CLUSTER_ID"})),
Usage: "list cluster services",
},
{
Name: "create",
ArgsUsage: "CLUSTER_ID",
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "service name"},
cli.StringFlag{Name: "namespace", Usage: "service namespace"},
cli.StringFlag{Name: "version", Usage: "service version"},
cli.StringFlag{Name: "repoId", Usage: "repository ID"},
cli.StringFlag{Name: "gitRef", Usage: "git ref"},
cli.StringFlag{Name: "gitFolder", Usage: "git folder"},
cli.StringSliceFlag{
Name: "conf",
Usage: " config name value",
},
cli.StringFlag{Name: "configFile", Usage: "path for configuration file"},
},
Action: latestVersion(requireArgs(p.handleCreateClusterService, []string{"CLUSTER_ID"})),
Usage: "create cluster service",
},
{
Name: "update",
ArgsUsage: "SERVICE_ID",
Action: latestVersion(requireArgs(p.handleUpdateClusterService, []string{"SERVICE_ID"})),
Usage: "update cluster service",
Flags: []cli.Flag{
cli.StringFlag{Name: "version", Usage: "service version"},
cli.StringFlag{Name: "gitRef", Usage: "git ref"},
cli.StringFlag{Name: "gitFolder", Usage: "git folder"},
cli.StringSliceFlag{
Name: "conf",
Usage: " config name value",
},
},
},
}
}

Expand Down Expand Up @@ -120,9 +158,143 @@ func (p *Plural) handleListClusterServices(c *cli.Context) error {
return err
}

headers := []string{"Id", "Name", "Namespace", "Git Ref", "Git Folder"}
headers := []string{"Id", "Name", "Namespace", "Git Ref", "Git Folder", "Repo ID"}
return utils.PrintTable(sd.ServiceDeployments.Edges, headers, func(sd *gqlclient.ServiceDeploymentEdgeFragment) ([]string, error) {
return []string{sd.Node.ID, sd.Node.Name, sd.Node.Namespace, sd.Node.Git.Ref, sd.Node.Git.Folder}, nil
return []string{sd.Node.ID, sd.Node.Name, sd.Node.Namespace, sd.Node.Git.Ref, sd.Node.Git.Folder, sd.Node.Repository.ID}, nil
})
}

type ServiceDeploymentAttributesConfiguration struct {
Configuration []*gqlclient.ConfigAttributes
}

func (p *Plural) handleCreateClusterService(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
clusterId := c.Args().Get(0)
v := c.String("version")
attributes := gqlclient.ServiceDeploymentAttributes{
Name: c.String("name"),
Namespace: c.String("namespace"),
Version: &v,
RepositoryID: c.String("repoId"),
Git: gqlclient.GitRefAttributes{
Ref: c.String("gitRef"),
Folder: c.String("gitFolder"),
},
Configuration: []*gqlclient.ConfigAttributes{},
}

if c.String("configFile") != "" {
configFile, err := utils.ReadFile(c.String("configFile"))
if err != nil {
return err
}
sdc := ServiceDeploymentAttributesConfiguration{}
if err := yaml.Unmarshal([]byte(configFile), &sdc); err != nil {
return err
}
attributes.Configuration = append(attributes.Configuration, sdc.Configuration...)
}
var confArgs []string
if c.IsSet("conf") {
confArgs = append(confArgs, c.StringSlice("conf")...)
}
for _, conf := range confArgs {
configurationPair := strings.Split(conf, "=")
if len(configurationPair) == 2 {
attributes.Configuration = append(attributes.Configuration, &gqlclient.ConfigAttributes{
Name: configurationPair[0],
Value: configurationPair[1],
})
}
}

sd, err := p.ConsoleClient.CreateClusterService(clusterId, attributes)
if err != nil {
return err
}
if sd == nil {
return fmt.Errorf("the returned object is empty, check if all fields are set")
}

headers := []string{"Id", "Name", "Namespace", "Git Ref", "Git Folder", "Repo"}
return utils.PrintTable([]*gqlclient.CreateServiceDeployment{sd}, headers, func(sd *gqlclient.CreateServiceDeployment) ([]string, error) {
return []string{sd.CreateServiceDeployment.ID, sd.CreateServiceDeployment.Name, sd.CreateServiceDeployment.Namespace, sd.CreateServiceDeployment.Git.Ref, sd.CreateServiceDeployment.Git.Folder, sd.CreateServiceDeployment.Repository.ID}, nil
})
}

func (p *Plural) handleUpdateClusterService(c *cli.Context) error {
if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil {
return err
}
serviceId := c.Args().Get(0)

existing, err := p.ConsoleClient.GetClusterService(serviceId)
if err != nil {
return err
}
if existing == nil {
return fmt.Errorf("existing service deployment is empty")
}
existingConfigurations := map[string]string{}
attributes := gqlclient.ServiceUpdateAttributes{
Version: &existing.ServiceDeployment.Version,
Git: gqlclient.GitRefAttributes{
Ref: existing.ServiceDeployment.Git.Ref,
Folder: existing.ServiceDeployment.Git.Folder,
},
Configuration: []*gqlclient.ConfigAttributes{},
}

for _, conf := range existing.ServiceDeployment.Configuration {
existingConfigurations[conf.Name] = conf.Value
}

v := c.String("version")
if v != "" {
attributes.Version = &v
}
if c.String("gitRef") != "" {
attributes.Git.Ref = c.String("gitRef")
}
if c.String("gitFolder") != "" {
attributes.Git.Folder = c.String("gitFolder")
}
var confArgs []string
if c.IsSet("conf") {
confArgs = append(confArgs, c.StringSlice("conf")...)
}

updateConfigurations := map[string]string{}
for _, conf := range confArgs {
configurationPair := strings.Split(conf, "=")
if len(configurationPair) == 2 {
updateConfigurations[configurationPair[0]] = configurationPair[1]
}
}
for k, v := range updateConfigurations {
existingConfigurations[k] = v
}
for key, value := range existingConfigurations {
attributes.Configuration = append(attributes.Configuration, &gqlclient.ConfigAttributes{
Name: key,
Value: value,
})
}

sd, err := p.ConsoleClient.UpdateClusterService(serviceId, attributes)
if err != nil {
return err
}
if sd == nil {
return fmt.Errorf("returned object is nil")
}

headers := []string{"Id", "Name", "Namespace", "Git Ref", "Git Folder", "Repo"}
return utils.PrintTable([]*gqlclient.UpdateServiceDeployment{sd}, headers, func(sd *gqlclient.UpdateServiceDeployment) ([]string, error) {
return []string{sd.UpdateServiceDeployment.ID, sd.UpdateServiceDeployment.Name, sd.UpdateServiceDeployment.Namespace, sd.UpdateServiceDeployment.Git.Ref, sd.UpdateServiceDeployment.Git.Folder, sd.UpdateServiceDeployment.Repository.ID}, nil
})
}

Expand All @@ -138,7 +310,7 @@ func (p *Plural) handleListClusters(c *cli.Context) error {

headers := []string{"Id", "Name", "Version"}
return utils.PrintTable(clusters.Clusters.Edges, headers, func(cl *gqlclient.ClusterEdgeFragment) ([]string, error) {
return []string{cl.Node.ID, cl.Node.Name, cl.Node.Version}, nil
return []string{cl.Node.ID, cl.Node.Name, *cl.Node.Version}, nil
})
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ require (
github.com/packethost/packngo v0.29.0
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pluralsh/cluster-api-migration v0.2.15
github.com/pluralsh/console-client-go v0.0.8
github.com/pluralsh/console-client-go v0.0.9
github.com/pluralsh/gqlclient v1.10.0
github.com/pluralsh/plural-operator v0.5.5
github.com/pluralsh/polly v0.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1389,8 +1389,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pluralsh/cluster-api-migration v0.2.15 h1:TIfusD+wnhZTGmwNfIlKlKJOT2dE3rUaZawDJw98GjY=
github.com/pluralsh/cluster-api-migration v0.2.15/go.mod h1:J6lEvC/70KouikX16mE331cxc3y3sBwtmfHGwZqu06w=
github.com/pluralsh/console-client-go v0.0.8 h1:BwWOt1ggBX/fxzY2+01dk8sBTz1jqT57o2y1Iz9Zxzk=
github.com/pluralsh/console-client-go v0.0.8/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU=
github.com/pluralsh/console-client-go v0.0.9 h1:otNj+tBuN9AsUU0i7+1BzwUCtPBFa245b6+FDnyN55o=
github.com/pluralsh/console-client-go v0.0.9/go.mod h1:kZjk0pXAWnvyj+miXveCho4kKQaX1Tm3CGAM+iwurWU=
github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxDz4Q2VMpzprJIIKShxqG0E=
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
github.com/pluralsh/gqlclient v1.10.0 h1:ccYB+A0JbPYkEeVzdfajd29l65N6x/buSKPMMxM8OIA=
Expand Down
3 changes: 3 additions & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type ConsoleClient interface {
ListClusterServices(clusterId string) (*consoleclient.ListServiceDeployment, error)
CreateRepository(url string, privateKey, passphrase, username, password *string) (*consoleclient.CreateGitRepository, error)
ListRepositories() (*consoleclient.ListGitRepositories, error)
CreateClusterService(clusterId string, attr consoleclient.ServiceDeploymentAttributes) (*consoleclient.CreateServiceDeployment, error)
UpdateClusterService(serviceId string, attr consoleclient.ServiceUpdateAttributes) (*consoleclient.UpdateServiceDeployment, error)
GetClusterService(serviceId string) (*consoleclient.GetServiceDeployment, error)
}

func NewConsoleClient(token, url string) (ConsoleClient, error) {
Expand Down
27 changes: 27 additions & 0 deletions pkg/console/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,30 @@ func (c *consoleClient) ListClusterServices(clusterId string) (*gqlclient.ListSe

return result, nil
}

func (c *consoleClient) CreateClusterService(clusterId string, attributes gqlclient.ServiceDeploymentAttributes) (*gqlclient.CreateServiceDeployment, error) {
result, err := c.client.CreateServiceDeployment(c.ctx, clusterId, attributes)
if err != nil {
return nil, api.GetErrorResponse(err, "CreateServiceDeployment")
}

return result, nil
}

func (c *consoleClient) UpdateClusterService(serviceId string, attributes gqlclient.ServiceUpdateAttributes) (*gqlclient.UpdateServiceDeployment, error) {
result, err := c.client.UpdateServiceDeployment(c.ctx, serviceId, attributes)
if err != nil {
return nil, api.GetErrorResponse(err, "UpdateClusterService")
}

return result, nil
}

func (c *consoleClient) GetClusterService(serviceId string) (*gqlclient.GetServiceDeployment, error) {
result, err := c.client.GetServiceDeployment(c.ctx, serviceId)
if err != nil {
return nil, api.GetErrorResponse(err, "UpdateClusterService")
}

return result, nil
}

0 comments on commit 34f3397

Please sign in to comment.