Skip to content

Commit

Permalink
Fix scheme for controller manager & refactor way to get API server to…
Browse files Browse the repository at this point in the history
…ken for bootstrap script (#180)

* Fix scheme for controller manager & refactor way to get API server token for bootstrap script

* Fix tests and update testdata

Signed-off-by: Waleed Malik <[email protected]>
  • Loading branch information
ahmedwaleedmalik authored Jun 29, 2022
1 parent 3d9712f commit cef3da9
Show file tree
Hide file tree
Showing 30 changed files with 4,951 additions and 161 deletions.
2 changes: 2 additions & 0 deletions cmd/osm-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func main() {
}

workerMgr, err = manager.New(workerClusterConfig, manager.Options{
Scheme: scheme,
LeaderElection: opt.enableLeaderElection,
LeaderElectionID: "operating-system-manager-worker-manager",
// We use hard-coded namespace kube-system here since manager uses worker cluster config
Expand Down Expand Up @@ -285,6 +286,7 @@ func main() {
func createManager(opt *options) (manager.Manager, error) {
// Manager options
options := manager.Options{
Scheme: scheme,
LeaderElection: opt.enableLeaderElection,
LeaderElectionID: "operating-system-manager",
LeaderElectionNamespace: opt.namespace,
Expand Down
9 changes: 9 additions & 0 deletions deploy/cloud-init-settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ metadata:
name: cloud-init-getter
namespace: cloud-init-settings
---
apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
name: cloud-init-getter-token
namespace: cloud-init-settings
annotations:
kubernetes.io/service-account.name: "cloud-init-getter"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ spec:
kind: OperatingSystemConfig
listKind: OperatingSystemConfigList
plural: operatingsystemconfigs
shortNames:
- osc
singular: operatingsystemconfig
scope: Namespaced
versions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ spec:
kind: OperatingSystemProfile
listKind: OperatingSystemProfileList
plural: operatingsystemprofiles
shortNames:
- osp
singular: operatingsystemprofile
scope: Namespaced
versions:
Expand Down
1 change: 1 addition & 0 deletions deploy/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ rules:
- ""
resources:
- configmaps
- secrets
verbs:
- get
- create
Expand Down
25 changes: 9 additions & 16 deletions pkg/bootstrap/cloud_init_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,26 @@ import (
"context"
"errors"
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)

const (
CloudInitNamespace = "cloud-init-settings"
jwtTokenNamePrefix = "cloud-init-getter-token"
CloudInitNamespace = "cloud-init-settings"
cloudInitGetterSecret = "cloud-init-getter-token"
)

func ExtractAPIServerToken(ctx context.Context, client ctrlruntimeclient.Client) (string, error) {
secretList := corev1.SecretList{}
if err := client.List(ctx, &secretList, &ctrlruntimeclient.ListOptions{Namespace: CloudInitNamespace}); err != nil {
return "", fmt.Errorf("failed to list secrets in namespace %s: %w", CloudInitNamespace, err)
secret := &corev1.Secret{}
if err := client.Get(ctx, types.NamespacedName{Name: cloudInitGetterSecret, Namespace: CloudInitNamespace}, secret); err != nil {
return "", fmt.Errorf("failed to get %s secrets in namespace %s: %w", cloudInitGetterSecret, CloudInitNamespace, err)
}

for _, secret := range secretList.Items {
if strings.HasPrefix(secret.Name, jwtTokenNamePrefix) {
if secret.Data != nil {
jwtToken := secret.Data["token"]
if jwtToken != nil {
token := string(jwtToken)
return token, nil
}
}
}
token := secret.Data["token"]
if token != nil {
return string(token), nil
}

return "", errors.New("failed to fetch api server token")
Expand Down
290 changes: 145 additions & 145 deletions pkg/controllers/osc/osc_reconciler_test.go

Large diffs are not rendered by default.

Loading

0 comments on commit cef3da9

Please sign in to comment.