Skip to content

Commit

Permalink
Merge branch 'kubesaw170_restart' of https://github.com/fbm3307/ksctl
Browse files Browse the repository at this point in the history
…into kubesaw170_restart
  • Loading branch information
fbm3307 committed Sep 17, 2024
2 parents ba8866e + c742197 commit ad5348e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 8 deletions.
31 changes: 30 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ serviceAccounts:
- ...
```

===== ServiceAccount namespace location

By default, all `ServiceAccounts` are created in default namespaces:

* `kubesaw-admins-host` for the host cluster
* `kubesaw-admins-meber` for the member cluster

The default location can be changed in `kubesaw-admin.yaml` file:

```yaml
defaultServiceAccountsNamespace:
host: your-host-namespace
member: your-member-namespace
```

These two namespaces has to have different names.

It's also possible to override the namespace location for a given `ServiceAccount`:

```yaml
serviceAccounts:
- name: in-namespace-sa
namespace: specific-sa-namespace
host:
...
member:
...
```

===== Generate ksctl.yaml files

For each ServiceAccount defined in this section, the `ksctl generate cli-configs` generates a separate `ksctl.yaml` file with the corresponding cluster configuration and tokens. As an administrator of the clusters, run this command and distribute securely the generated `ksctl.yaml` files to other team members.
Expand All @@ -201,7 +230,7 @@ For each ServiceAccount defined in this section, the `ksctl generate cli-configs
1. Run `make install`
2. Create `kubesaw-admins.yaml` (as an example, check link:test-resources/dummy.openshiftapps.com/kubesaw-admins.yaml[kubesaw-admins.yaml])
3. Run `ksctl generate admin-manifests --kubesaw-admins <path>/kubesaw-admins.yaml --out-dir <admin-manifests-out-dir-path>`
4. Create resources from the `<admin-manifests-out-dir-path>` of the previous command. Please, note that you will need to create some namespaces manually (`oc create ns <namespace-name>`), such as `sandbox-sre-host`, `first-component`, `second-component`, `some-component`, `sandbox-sre-member`, and `crw`, for example.
4. Create resources from the `<admin-manifests-out-dir-path>` of the previous command. Please, note that you will need to create some namespaces manually (`oc create ns <namespace-name>`), such as `host-sre-namespace`, `first-component`, `second-component`, `some-component`, `member-sre-namespace`, and `crw`, for example.
- Run `oc apply -k <admin-manifests-out-dir-path>/host`
- Run `oc apply -k <admin-manifests-out-dir-path>/member`
- Run `oc apply -k <admin-manifests-out-dir-path>/member-3`
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/adm/adm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func NewAdmCmd() *cobra.Command {
admCommand := &cobra.Command{
Use: "adm",
Short: "Administrative Commands",
Long: `Actions for administering Dev Sandbox instance.`,
Long: `Actions for administering a KubeSaw instance.`,
}
registerCommands(admCommand)

Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/generate/admin-manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

type adminManifestsFlags struct {
kubeSawAdminsFile, outDir, hostRootDir, memberRootDir string
singleCluster bool
kubeSawAdminsFile, outDir, hostRootDir, memberRootDir, idpName string
singleCluster bool
}

func NewAdminManifestsCmd() *cobra.Command {
Expand All @@ -39,6 +39,7 @@ ksctl generate admin-manifests ./path/to/kubesaw-stage.openshiftapps.com/kubesaw
command.Flags().BoolVarP(&f.singleCluster, "single-cluster", "s", false, "If host and member are deployed to the same cluster. Cannot be used with separateKustomizeComponent set in one of the members.")
command.Flags().StringVar(&f.hostRootDir, "host-root-dir", "host", "The root directory name for host manifests")
command.Flags().StringVar(&f.memberRootDir, "member-root-dir", "member", "The root directory name for member manifests")
command.Flags().StringVar(&f.idpName, "idp-name", "KubeSaw", "Identity provider name to be used in Identity CRs")

flags.MustMarkRequired(command, "kubesaw-admins")
flags.MustMarkRequired(command, "out-dir")
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/generate/admin-manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func newAdminManifestsFlags(adminManifestsFlagsOptions ...adminManifestsFlagsOpt
flags := adminManifestsFlags{
hostRootDir: "host",
memberRootDir: "member",
idpName: "KubeSaw",
}
for _, applyOption := range adminManifestsFlagsOptions {
applyOption(&flags)
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/generate/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ func (a *storageAssertionImpl) assertUser(name string) userAssertion {
}

func (a userAssertion) hasIdentity(ID string) userAssertion {
ins := commonidentity.NewIdentityNamingStandard(ID, "DevSandbox")
return a.hasIdentityWithIdentityStandard(commonidentity.NewIdentityNamingStandard(ID, "KubeSaw"))
}

func (a userAssertion) hasIdentityWithIdentityStandard(ins commonidentity.NamingStandard) userAssertion {
src := &userv1.Identity{}
ins.ApplyToIdentity(src)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/generate/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ensureServiceAccounts(ctx *clusterContext, objsCache objectsCache) error {
continue
}

// by default, it should use the sandbox sre namespace. let's keep this empty (if the target namespace is not defined) so it is recognized in the ensureServiceAccount method based on the cluster type it is being applied in
// let's keep this empty (if the target namespace is not defined) so it is recognized in the ensureServiceAccount method based on the cluster type it is being applied in
saNamespace := ""
if sa.Namespace != "" {
saNamespace = sa.Namespace
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/generate/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func newAdminManifestsContext(t *testing.T, config *assets.KubeSawAdmins, files
outDir: temp,
memberRootDir: "member",
hostRootDir: "host",
idpName: "KubeSaw",
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/generate/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func ensureUserIdentityAndGroups(IDs []string, groups []string) newSubjectFunc {
// Create identities and identity mappings
for _, id := range IDs {

ins := commonidentity.NewIdentityNamingStandard(id, "DevSandbox")
ins := commonidentity.NewIdentityNamingStandard(id, ctx.idpName)

// create identity
identity := &userv1.Identity{
Expand Down
23 changes: 22 additions & 1 deletion pkg/cmd/generate/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

commonidentity "github.com/codeready-toolchain/toolchain-common/pkg/identity"
commontest "github.com/codeready-toolchain/toolchain-common/pkg/test"
"github.com/kubesaw/ksctl/pkg/assets"
"github.com/kubesaw/ksctl/pkg/client"
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestEnsureServiceAccount(t *testing.T) {

// when
subject, err := ensureServiceAccount("openshift-customer-monitoring")(
ctx, cache, "john", "sandbox-sre-host", labels)
ctx, cache, "john", "default-ns-is-ignored", labels)

// then
require.NoError(t, err)
Expand Down Expand Up @@ -148,6 +149,26 @@ func TestEnsureUserAndIdentity(t *testing.T) {
assert.Empty(t, subject.Namespace)
})

t.Run("create user & identity with custom IdP", func(t *testing.T) {
// given
ctx := newFakeClusterContext(newAdminManifestsContextWithDefaultFiles(t, nil), configuration.Host)
ctx.idpName = "MyIdP"
cache := objectsCache{}

// when
subject, err := ensureUserIdentityAndGroups([]string{"12345", "abc:19944:FZZ"}, []string{})(ctx, cache, "john-crtadmin", commontest.HostOperatorNs, labels)

// then
require.NoError(t, err)
inObjectCache(t, ctx.outDir, "host", cache).
assertUser("john-crtadmin").
hasIdentityWithIdentityStandard(commonidentity.NewIdentityNamingStandard("12345", "MyIdP")).
hasIdentityWithIdentityStandard(commonidentity.NewIdentityNamingStandard("abc:19944:FZZ", "MyIdP"))
assert.Equal(t, "User", subject.Kind)
assert.Equal(t, "john-crtadmin", subject.Name)
assert.Empty(t, subject.Namespace)
})

t.Run("don't create any group", func(t *testing.T) {
// given
ctx := newFakeClusterContext(newAdminManifestsContextWithDefaultFiles(t, nil), configuration.Host)
Expand Down

0 comments on commit ad5348e

Please sign in to comment.