Skip to content

Commit

Permalink
fix(internal/jimm/controller.go): add default cloud region if none exist
Browse files Browse the repository at this point in the history
When adding a controller JIMM will query the controller for all known clouds and then add those
clouds to its internal DB. If a cloud (e.g. MAAS or k8s) has no regions, JIMM would be unable to add
a model to that cloud as it does not store association between clouds and controllers. To solve the
issue we add a `default` region to clouds that do not have any regions and that cloud region will
only have one controller associated with it.
  • Loading branch information
alesstimec committed Jan 9, 2025
1 parent 4ee7344 commit 081cb58
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/jimm/applicationoffer_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/audit_log_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down
14 changes: 13 additions & 1 deletion internal/jimm/controller.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm

Expand Down Expand Up @@ -251,6 +251,18 @@ func (j *JIMM) AddController(ctx context.Context, user *openfga.User, ctl *dbmod
}

dbClouds := convertJujuCloudsToDbClouds(clouds)
for i, cloud := range dbClouds {
if len(cloud.Regions) == 0 {
dbClouds[i].Regions = []dbmodel.CloudRegion{{
CloudName: cloud.Name,
Name: "default",
}}
if cloud.Name == ctl.CloudName {
ctl.CloudRegion = "default"
}

}
}

// TODO(ale8k): This shouldn't be necessary to check, but tests need updating
// to set insecure credential store explicitly.
Expand Down
90 changes: 89 additions & 1 deletion internal/jimm/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down Expand Up @@ -176,6 +176,94 @@ func TestAddController(t *testing.T) {
c.Check(ctl4, qt.CmpEquals(cmpopts.EquateEmpty(), cmpopts.IgnoreTypes(dbmodel.CloudRegion{})), ctl3)
}

func TestAddControllerWithCloudWithoutRegions(t *testing.T) {
c := qt.New(t)

api := &jimmtest.API{
Clouds_: func(context.Context) (map[names.CloudTag]jujuparams.Cloud, error) {
clouds := map[names.CloudTag]jujuparams.Cloud{
names.NewCloudTag("k8s"): {
Type: "kubernetes",
AuthTypes: []string{"userpass"},
Endpoint: "https://k8s.example.com",
},
}
return clouds, nil
},
CloudInfo_: func(_ context.Context, tag names.CloudTag, ci *jujuparams.CloudInfo) error {
if tag.Id() != "k8s" {
c.Errorf("CloudInfo called for unexpected cloud %q", tag)
return errors.E("unexpected cloud")
}
ci.Type = "kubernetes"
ci.AuthTypes = []string{"userpass"}
ci.Endpoint = "https://k8s.example.com"
return nil
},
ControllerModelSummary_: func(_ context.Context, ms *jujuparams.ModelSummary) error {
ms.Name = "controller"
ms.UUID = "5fddf0ed-83d5-47e8-ae7b-a4b27fc04a9f"
ms.Type = "iaas"
ms.ControllerUUID = jimmtest.DefaultControllerUUID
ms.IsController = true
ms.ProviderType = "ec2"
ms.DefaultSeries = "warty"
ms.CloudTag = "cloud-k8s"
ms.OwnerTag = "user-admin"
ms.Life = life.Value(state.Alive.String())
ms.Status = jujuparams.EntityStatus{
Status: "available",
}
ms.UserAccess = "admin"
ms.AgentVersion = newVersion("1.2.3")
return nil
},
}

j := jimmtest.NewJIMM(c, &jimm.Parameters{
Dialer: &jimmtest.Dialer{
API: api,
},
})

ctx := context.Background()

u, err := dbmodel.NewIdentity("alice@canonical.com")
c.Assert(err, qt.IsNil)

alice := openfga.NewUser(u, j.OpenFGAClient)
alice.JimmAdmin = true
err = alice.SetControllerAccess(context.Background(), j.ResourceTag(), ofganames.AdministratorRelation)
c.Assert(err, qt.IsNil)

ctl1 := dbmodel.Controller{
Name: "test-controller",
AdminIdentityName: "admin",
AdminPassword: "5ecret",
PublicAddress: "example.com:443",
}
err = j.AddController(context.Background(), alice, &ctl1)
c.Assert(err, qt.IsNil)

ctl2 := dbmodel.Controller{
Name: "test-controller",
}
err = j.Database.GetController(ctx, &ctl2)
c.Assert(err, qt.IsNil)
c.Check(ctl2, qt.CmpEquals(cmpopts.EquateEmpty(), cmpopts.IgnoreTypes(dbmodel.CloudRegion{})), ctl1)
c.Check(ctl2.CloudRegion, qt.Equals, "default")

cloud := dbmodel.Cloud{
Name: "k8s",
}
err = j.Database.GetCloud(ctx, &cloud)
c.Assert(err, qt.IsNil)
c.Assert(cloud.Regions, qt.HasLen, 1)
c.Assert(cloud.Regions[0].Name, qt.Equals, "default")
c.Assert(cloud.Regions[0].Controllers, qt.HasLen, 1)
c.Assert(cloud.Regions[0].Controllers[0].Controller.Name, qt.Equals, ctl1.Name)
}

func TestAddControllerWithVault(t *testing.T) {
c := qt.New(t)

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/export_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/group/group_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package group_test

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/jimm_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/model_cleanup_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/model_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/role/role_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package role_test

Expand Down
2 changes: 1 addition & 1 deletion internal/jimm/watcher_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2024 Canonical.
// Copyright 2025 Canonical.

package jimm_test

Expand Down

0 comments on commit 081cb58

Please sign in to comment.