Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster names #1069

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions cmd/cloud/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func newCmdClusterUpdate() *cobra.Command {
},
PreRun: func(cmd *cobra.Command, args []string) {
flags.clusterFlags.addFlags(cmd)
flags.clusterPatchRequestChanges.addFlags(cmd)
},
}
flags.addFlags(cmd)
Expand All @@ -314,12 +315,9 @@ func newCmdClusterUpdate() *cobra.Command {
}

func executeClusterUpdateCmd(flags clusterUpdateFlags) error {

client := createClient(flags.clusterFlags)

request := &model.UpdateClusterRequest{
AllowInstallations: flags.allowInstallations,
}
request := flags.GetPatchClusterRequest()

if flags.dryRun {
return runDryRun(request)
Expand Down Expand Up @@ -595,7 +593,7 @@ func executeClusterListCmd(flags clusterListFlags) error {
}

func defaultClustersTableData(clusters []*model.ClusterDTO) ([]string, [][]string) {
keys := []string{"ID", "STATE", "VERSION", "MASTER NODES", "WORKER NODES", "AMI ID/NAME", "NETWORKING", "VPC", "STATUS"}
keys := []string{"ID", "NAME", "STATE", "VERSION", "MASTER NODES", "WORKER NODES", "AMI ID/NAME", "NETWORKING", "VPC", "STATUS"}
values := make([][]string, 0, len(clusters))

for _, cluster := range clusters {
Expand Down Expand Up @@ -632,7 +630,7 @@ func defaultClustersTableData(clusters []*model.ClusterDTO) ([]string, [][]strin
}

values = append(values, []string{
cluster.ID, cluster.State,
cluster.ID, cluster.Name, cluster.State,
versionEntry, masterEntry, workerEntry, amiEntry, networkingEntry, vpcEntry,
status,
})
Expand Down
26 changes: 26 additions & 0 deletions cmd/cloud/cluster_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,45 @@ func (flags *pgBouncerConfigOptions) GetPatchPgBouncerConfig() *model.PatchPgBou
return &request
}

func (flags *clusterPatchRequestChanges) addFlags(command *cobra.Command) {
flags.nameChanged = command.Flags().Changed("name")
flags.allowInstallationsChanged = command.Flags().Changed("allow-installations")
}

type clusterPatchRequestChanges struct {
nameChanged bool
allowInstallationsChanged bool
}

type clusterUpdateFlags struct {
clusterFlags
clusterPatchRequestChanges
cluster string
name string
allowInstallations bool
}

func (flags *clusterUpdateFlags) addFlags(command *cobra.Command) {
command.Flags().StringVar(&flags.cluster, "cluster", "", "The id of the cluster to be updated.")
command.Flags().StringVar(&flags.name, "name", "", "An optional name value to identify the cluster.")
command.Flags().BoolVar(&flags.allowInstallations, "allow-installations", true, "Whether the cluster will allow for new installations to be scheduled.")

_ = command.MarkFlagRequired("cluster")
}

func (flags *clusterUpdateFlags) GetPatchClusterRequest() *model.UpdateClusterRequest {
request := model.UpdateClusterRequest{}

if flags.nameChanged {
request.Name = &flags.name
}
if flags.allowInstallationsChanged {
request.AllowInstallations = &flags.allowInstallations
}

return &request
}

type rotatorConfig struct {
useRotator bool
maxScaling int
Expand Down
3 changes: 1 addition & 2 deletions internal/api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,7 @@ func handleUpdateClusterConfiguration(c *Context, w http.ResponseWriter, r *http
return
}

if clusterDTO.AllowInstallations != updateClusterRequest.AllowInstallations {
clusterDTO.AllowInstallations = updateClusterRequest.AllowInstallations
if clusterDTO.ApplyClusterUpdatePatch(updateClusterRequest) {
err := c.Store.UpdateCluster(clusterDTO.Cluster)
if err != nil {
c.Logger.WithError(err).Error("failed to update cluster")
Expand Down
18 changes: 16 additions & 2 deletions internal/api/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,19 +931,33 @@ func TestUpdateClusterConfiguration(t *testing.T) {
require.NoError(t, errTest)
})

t.Run("while stable", func(t *testing.T) {
t.Run("while stable, allow installations", func(t *testing.T) {
cluster1.State = model.ClusterStateStable
errTest := sqlStore.UpdateCluster(cluster1.Cluster)
require.NoError(t, errTest)

clusterResp, errTest := client.UpdateCluster(cluster1.ID, &model.UpdateClusterRequest{AllowInstallations: false})
clusterResp, errTest := client.UpdateCluster(cluster1.ID, &model.UpdateClusterRequest{AllowInstallations: util.BToP(false)})
require.NoError(t, errTest)
assert.NotNil(t, clusterResp)

cluster2, errTest := client.GetCluster(cluster1.ID)
require.NoError(t, errTest)
assert.Equal(t, model.ClusterStateStable, cluster2.State)
assert.False(t, cluster2.AllowInstallations)
assert.Empty(t, cluster2.Name)
assert.True(t, containsAnnotation("my-annotation", cluster2.Annotations))
})

t.Run("while stable, change name", func(t *testing.T) {
clusterResp, errTest := client.UpdateCluster(cluster1.ID, &model.UpdateClusterRequest{Name: util.SToP("newname")})
require.NoError(t, errTest)
assert.NotNil(t, clusterResp)

cluster2, errTest := client.GetCluster(cluster1.ID)
require.NoError(t, errTest)
assert.Equal(t, model.ClusterStateStable, cluster2.State)
assert.False(t, cluster2.AllowInstallations)
assert.Equal(t, "newname", cluster2.Name)
assert.True(t, containsAnnotation("my-annotation", cluster2.Annotations))
})
}
Expand Down
4 changes: 3 additions & 1 deletion internal/store/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var clusterSelect sq.SelectBuilder

func init() {
clusterSelect = sq.
Select("Cluster.ID", "Provider", "Provisioner", "ProviderMetadataRaw", "ProvisionerMetadataRaw",
Select("Cluster.ID", "Name", "Provider", "Provisioner", "ProviderMetadataRaw", "ProvisionerMetadataRaw",
"UtilityMetadataRaw", "PgBouncerConfig", "State", "AllowInstallations", "CreateAt", "DeleteAt",
"APISecurityLock", "SchedulingLockAcquiredBy", "SchedulingLockAcquiredAt", "LockAcquiredBy", "LockAcquiredAt").
From("Cluster")
Expand Down Expand Up @@ -256,6 +256,7 @@ func (sqlStore *SQLStore) createCluster(execer execer, cluster *model.Cluster) e
Insert("Cluster").
SetMap(map[string]interface{}{
"ID": cluster.ID,
"Name": cluster.Name,
"State": cluster.State,
"Provider": cluster.Provider,
"ProviderMetadataRaw": rawMetadata.ProviderMetadataRaw,
Expand Down Expand Up @@ -290,6 +291,7 @@ func (sqlStore *SQLStore) UpdateCluster(cluster *model.Cluster) error {
Update("Cluster").
SetMap(map[string]interface{}{
"State": cluster.State,
"Name": cluster.Name,
"Provider": cluster.Provider,
"ProviderMetadataRaw": rawMetadata.ProviderMetadataRaw,
"Provisioner": cluster.Provisioner,
Expand Down
8 changes: 8 additions & 0 deletions internal/store/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,14 @@ var migrations = []migration{
return err
}

return nil
}},
{semver.MustParse("0.48.0"), semver.MustParse("0.49.0"), func(e execer) error {
_, err := e.Exec(`ALTER TABLE Cluster ADD COLUMN Name TEXT NOT NULL DEFAULT '';`)
if err != nil {
return err
}

return nil
}},
}
15 changes: 15 additions & 0 deletions model/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// Cluster represents a Kubernetes cluster.
type Cluster struct {
ID string
Name string
State string
Provider string
ProviderMetadataAWS *AWSMetadata `json:"ProviderMetadataAWS,omitempty"`
Expand Down Expand Up @@ -79,6 +80,20 @@ func (c *Cluster) HasAWSInfrastructure() bool {
return true
}

func (c *Cluster) ApplyClusterUpdatePatch(patchRequest *UpdateClusterRequest) bool {
var applied bool
if patchRequest.Name != nil && *patchRequest.Name != c.Name {
applied = true
c.Name = *patchRequest.Name
}
if patchRequest.AllowInstallations != nil && *patchRequest.AllowInstallations != c.AllowInstallations {
applied = true
c.AllowInstallations = *patchRequest.AllowInstallations
}

return applied
}

// ClusterFromReader decodes a json-encoded cluster from the given io.Reader.
func ClusterFromReader(reader io.Reader) (*Cluster, error) {
cluster := Cluster{}
Expand Down
3 changes: 2 additions & 1 deletion model/cluster_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ func (request *GetClustersRequest) ApplyToURL(u *url.URL) {

// UpdateClusterRequest specifies the parameters available for updating a cluster.
type UpdateClusterRequest struct {
AllowInstallations bool
Name *string
AllowInstallations *bool
}

// NewUpdateClusterRequestFromReader will create an UpdateClusterRequest from an io.Reader with JSON data.
Expand Down
54 changes: 54 additions & 0 deletions model/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"testing"

"github.com/mattermost/mattermost-cloud/internal/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -129,6 +130,59 @@ func TestClusterHasAWSInfrastructure(t *testing.T) {
}
}

func TestClusterApplyClusterUpdatePatch(t *testing.T) {
tests := []struct {
name string
cluster Cluster
patch *UpdateClusterRequest
expectedApply bool
expectedCluster Cluster
}{
{
"empty patch",
Cluster{},
&UpdateClusterRequest{},
false,
Cluster{},
},
{
"name only",
Cluster{},
&UpdateClusterRequest{Name: util.SToP("new1")},
true,
Cluster{Name: "new1"},
},
{
"allow installations only",
Cluster{},
&UpdateClusterRequest{AllowInstallations: util.BToP(true)},
true,
Cluster{AllowInstallations: true},
},
{
"all values",
Cluster{},
&UpdateClusterRequest{
Name: util.SToP("new2"),
AllowInstallations: util.BToP(true),
},
true,
Cluster{
Name: "new2",
AllowInstallations: true,
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
applied := test.cluster.ApplyClusterUpdatePatch(test.patch)
assert.Equal(t, test.expectedApply, applied)
assert.Equal(t, test.expectedCluster, test.cluster)
})
}
}

func TestClusterFromReader(t *testing.T) {
t.Run("empty request", func(t *testing.T) {
cluster, err := ClusterFromReader(bytes.NewReader([]byte(
Expand Down
Loading