Skip to content
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

Remove image pull secret path support #274

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 11 additions & 20 deletions pkg/collect/run.go
Original file line number Diff line number Diff line change
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
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, "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