Skip to content

WIP Extract key from new StorageClass SecretRefs attribue #30

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
58 changes: 55 additions & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
jsonv2 "encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/util/json"
"net"
Expand All @@ -29,14 +30,13 @@ import (

"github.com/kubernetes-incubator/external-storage/lib/controller"

"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"

"github.com/container-storage-interface/spec/lib/go/csi"
)

Expand Down Expand Up @@ -194,6 +194,35 @@ func NewCSIProvisioner(client kubernetes.Interface, csiEndpoint string, connecti
return provisioner
}

func printIndentedJson(data interface{}) string {
var indentedJSON []byte

indentedJSON, err := jsonv2.MarshalIndent(data, "", "\t")
if err != nil {
return fmt.Sprintf("JSON parse error: %v", err)
}
return string(indentedJSON)
}

func parseStorageClassSecret(secretName string, namespace string, c kubernetes.Interface) (string, error) {
if c == nil {
return "", fmt.Errorf("Cannot get kube client")
}
secrets, err := c.CoreV1().Secrets(namespace).Get(secretName, metav1.GetOptions{})
if err != nil {
return "", err
}
secret := ""
for k, v := range secrets.Data {
if k == secretName {
return string(v), nil
}
secret = string(v)
}

return secret, nil
}

func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.PersistentVolume, error) {
if options.PVC.Spec.Selector != nil {
return nil, fmt.Errorf("claim Selector is not supported")
Expand All @@ -203,6 +232,28 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
capacity := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
volSizeBytes := capacity.Value()

userCredentials := map[string]string{}
storageClassName := options.PVC.Spec.StorageClassName
fmt.Printf("><SB> provisioner was called for storage class: %s\n", *storageClassName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to remove fmt.Printf statements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will do.

storageClass, err := p.client.StorageV1().StorageClasses().Get(*storageClassName, metav1.GetOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate storageClassName

if err != nil {
glog.Warningf("failed to retrieve information about the storage class: %s with error: %v", storageClassName, err)
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uneccessary else-block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate if you could clarify why. The logic here is if we cannot get a storage class object, we just print warning and do not process any secret info, basically just skipping whatever is covered by else part.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like storageclass is optional. If so, then the code is good.

if storageClass.SecretRefs != nil {
// Now we can extract user names and corresponding keys
for user, secret := range storageClass.SecretRefs {
key, err := parseStorageClassSecret(secret.Name, secret.Namespace, p.client)
if err != nil {
glog.Warningf("failed to retrieve secret: %s/%s with error: %v", secret.Name, secret.Namespace, err)
continue
}
userCredentials[user] = key
fmt.Printf("><SB> Extracted user name: %s secret name: %s secret namespace: %s key: %s \n", user, secret.Name, secret.Namespace, key)
}
}
}
fmt.Printf("><SB> Storage class content: %s\n", printIndentedJson(storageClass))

// Create a CSI CreateVolumeRequest
req := csi.CreateVolumeRequest{
Name: share,
Expand All @@ -218,6 +269,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
RequiredBytes: uint64(volSizeBytes),
LimitBytes: uint64(volSizeBytes),
},
UserCredentials: userCredentials,
}
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
Expand Down
Loading