Skip to content

Commit

Permalink
Support for path to config.json file removed
Browse files Browse the repository at this point in the history
  • Loading branch information
manavellamnimble committed Oct 1, 2020
1 parent 7fe7216 commit e9ba73d
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions pkg/collect/run.go
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/base64"
"encoding/json"
"io/ioutil"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -175,35 +174,27 @@ func createSecret(ctx context.Context, client *kubernetes.Clientset, imagePullSe
var out bytes.Buffer
data := make(map[string][]byte)
if imagePullSecret.SecretType == "kubernetes.io/dockerconfigjson" {
//If secret type is dockerconfigjson, check if required field in data exists
//Check if required field in data exists
v, found := imagePullSecret.Data[".dockerconfigjson"]
if !found {
return errors.Errorf("Secret type kubernetes.io/dockerconfigjson requires argument \".dockerconfigjson\"")
}
if len(imagePullSecret.Data) > 1 {
return errors.Errorf("Secret type kubernetes.io/dockerconfigjson accepts only one argument \".dockerconfigjson\"")
}
//Then, if data is a path to a config file, it is opened
configFile, err := ioutil.ReadFile(v)
//K8s client accepts only Json formated files as data, provided data must be decoded and indented (indentation is required)
parsedConfig, err := base64.StdEncoding.DecodeString(v)
if err != nil {
//If data is not a valid path, we assume data is a base64 encoded config.json file
//Client only accepts Json formated files as data, so we decode and indent it (indentation is required)
parsedConfig, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return errors.Wrap(err, "Secret's config file not found or unable to decode data.")
}
err = json.Indent(&out, parsedConfig, "", "\t")
if err != nil {
return errors.Wrap(err, "Unable to parse encoded data.")
}
data[".dockerconfigjson"] = out.Bytes()
} else {
data[".dockerconfigjson"] = configFile
return errors.Wrap(err, "Secret's config file not found or unable to decode data.")
}
} else {
for k, v := range imagePullSecret.Data {
data[k] = []byte(v)
err = json.Indent(&out, parsedConfig, "", "\t")
if err != nil {
return errors.Wrap(err, "Unable to parse encoded data.")
}
data[".dockerconfigjson"] = out.Bytes()

} else {
return errors.Errorf("ImagePullSecret must be of type: kubernetes.io/dockerconfigjson")
}
secret := corev1.Secret{
TypeMeta: metav1.TypeMeta{
Expand Down

0 comments on commit e9ba73d

Please sign in to comment.