Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Check ClusterRole resources in cluster in configuration phase #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions pkg/configure_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (b *backend) handleConfigWrite(ctx context.Context, req *logical.Request, d
return logical.ErrorResponse("Configuration not valid: %s", err), err
}

// Check ClusterRole resources in k8s cluster
if err := b.kubernetesService.CheckClusterRoleResources(&config); err != nil {
return nil, fmt.Errorf("Error: %s", err)
}

entry, err := logical.StorageEntryJSON(configPath, config)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions pkg/kubernetes_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package servian

// KubernetesInterface defines the core functions for the Kubernetes integration
type KubernetesInterface interface {
// CheckClusterRoleResources checks if ClusterRole resource exists for each role
CheckClusterRoleResources(pluginConfig *PluginConfig) error

// CreateServiceAccount creates a new service account
CreateServiceAccount(pluginConfig *PluginConfig, namespace string) (*ServiceAccountDetails, error)

Expand Down
19 changes: 19 additions & 0 deletions pkg/kubernetes_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ const roleKind = "Role"
// KubernetesService is an empty struct to wrap the Kubernetes service functions
type KubernetesService struct{}

// CheckClusterRoleResources checks if ClusterRole resource exists for each role
func (k *KubernetesService) CheckClusterRoleResources(pluginConfig *PluginConfig) error {
clientSet, err := getClientSet(pluginConfig)
if err != nil {
return err
}

roles := []string{pluginConfig.AdminRole, pluginConfig.EditorRole, pluginConfig.ViewerRole}

for _, role := range roles {
_, err = clientSet.RbacV1().ClusterRoles().Get(role, metav1.GetOptions{})
if err != nil {
return err
}
}

return nil
}

// CreateServiceAccount creates a new service account
func (k *KubernetesService) CreateServiceAccount(pluginConfig *PluginConfig, namespace string) (*ServiceAccountDetails, error) {
clientSet, err := getClientSet(pluginConfig)
Expand Down