Skip to content

Commit c12e12b

Browse files
committed
fix unusable secret manifest for type docker-registry
1 parent e457683 commit c12e12b

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_docker.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/base64"
2222
"encoding/json"
2323
"fmt"
24+
"strings"
2425

2526
"github.com/spf13/cobra"
2627
corev1 "k8s.io/api/core/v1"
@@ -58,7 +59,7 @@ var (
5859
kubectl create secret docker-registry my-secret --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
5960
6061
# Create a new secret named my-secret from ~/.docker/config.json
61-
kubectl create secret docker-registry my-secret --from-file=.dockerconfigjson=path/to/.docker/config.json`))
62+
kubectl create secret docker-registry my-secret --from-file=path/to/.docker/config.json`))
6263
)
6364

6465
// DockerConfigJSON represents a local docker auth config file
@@ -152,7 +153,11 @@ func NewCmdCreateSecretDockerRegistry(f cmdutil.Factory, ioStreams genericioopti
152153
cmd.Flags().StringVar(&o.Email, "docker-email", o.Email, i18n.T("Email for Docker registry"))
153154
cmd.Flags().StringVar(&o.Server, "docker-server", o.Server, i18n.T("Server location for Docker registry"))
154155
cmd.Flags().BoolVar(&o.AppendHash, "append-hash", o.AppendHash, "Append a hash of the secret to its name.")
155-
cmd.Flags().StringSliceVar(&o.FileSources, "from-file", o.FileSources, "Key files can be specified using their file path, in which case a default name will be given to them, or optionally with a name and file path, in which case the given name will be used. Specifying a directory will iterate each named file in the directory that is a valid secret key.")
156+
cmd.Flags().StringSliceVar(&o.FileSources, "from-file", o.FileSources, "Key files can be specified using their file path, "+
157+
"in which case a default name of "+corev1.DockerConfigJsonKey+" will be given to them, "+
158+
"or optionally with a name and file path, in which case the given name will be used. "+
159+
"Specifying a directory will iterate each named file in the directory that is a valid secret key. "+
160+
"For this command, the key should always be "+corev1.DockerConfigJsonKey+".")
156161

157162
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
158163

@@ -204,6 +209,11 @@ func (o *CreateSecretDockerRegistryOptions) Complete(f cmdutil.Factory, cmd *cob
204209
return err
205210
}
206211

212+
for i := range o.FileSources {
213+
if !strings.Contains(o.FileSources[i], "=") {
214+
o.FileSources[i] = corev1.DockerConfigJsonKey + "=" + o.FileSources[i]
215+
}
216+
}
207217
return nil
208218
}
209219

staging/src/k8s.io/kubectl/pkg/cmd/create/create_secret_docker_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ limitations under the License.
1717
package create
1818

1919
import (
20+
"encoding/json"
21+
"fmt"
22+
"os"
2023
"testing"
2124

2225
corev1 "k8s.io/api/core/v1"
2326
apiequality "k8s.io/apimachinery/pkg/api/equality"
2427
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/cli-runtime/pkg/genericiooptions"
29+
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
2530
)
2631

2732
func TestCreateSecretDockerRegistry(t *testing.T) {
@@ -183,3 +188,80 @@ func TestCreateSecretDockerRegistry(t *testing.T) {
183188
})
184189
}
185190
}
191+
192+
func TestCreateSecretDockerRegistryFromFile(t *testing.T) {
193+
username, password, email, server := "test-user", "test-password", "[email protected]", "https://index.docker.io/v1/"
194+
secretData, err := handleDockerCfgJSONContent(username, password, email, server)
195+
if err != nil {
196+
t.Errorf("unexpected error: %v", err)
197+
}
198+
secret := &corev1.Secret{
199+
TypeMeta: metav1.TypeMeta{
200+
APIVersion: corev1.SchemeGroupVersion.String(),
201+
Kind: "Secret",
202+
},
203+
ObjectMeta: metav1.ObjectMeta{
204+
Name: "foo",
205+
},
206+
Type: corev1.SecretTypeDockerConfigJson,
207+
Data: map[string][]byte{
208+
corev1.DockerConfigJsonKey: secretData,
209+
},
210+
}
211+
212+
tests := map[string]struct {
213+
withKey bool
214+
expected *corev1.Secret
215+
}{
216+
"create_secret_docker_registry_from_file_with_keyname": {
217+
withKey: true,
218+
expected: secret,
219+
},
220+
"create_secret_docker_registry_from_file_without_keyname": {
221+
withKey: false,
222+
expected: secret,
223+
},
224+
}
225+
226+
// Run all the tests
227+
for name, test := range tests {
228+
t.Run(name, func(t *testing.T) {
229+
tmp, _ := os.MkdirTemp("", "input")
230+
defer func() {
231+
err := os.RemoveAll(tmp)
232+
if err != nil {
233+
t.Fatalf("Failed to teardown: %s", err)
234+
}
235+
}()
236+
dockerCfgFile := tmp + "/dockerconfig.json"
237+
err := os.WriteFile(dockerCfgFile, secretData, 0644)
238+
if err != nil {
239+
t.Errorf("unexpected error: %v", err)
240+
}
241+
242+
tf := cmdtesting.NewTestFactory()
243+
defer tf.Cleanup()
244+
ioStreams, _, out, _ := genericiooptions.NewTestIOStreams()
245+
cmd := NewCmdCreateSecretDockerRegistry(tf, ioStreams)
246+
args := []string{"foo", "--dry-run=client", "-ojson"}
247+
if test.withKey {
248+
args = append(args, fmt.Sprintf("--from-file=%s=%s", corev1.DockerConfigJsonKey, dockerCfgFile))
249+
} else {
250+
args = append(args, fmt.Sprintf("--from-file=%s", dockerCfgFile))
251+
}
252+
cmd.SetArgs(args)
253+
err = cmd.Execute()
254+
if err != nil {
255+
t.Errorf("unexpected error: %v", err)
256+
}
257+
got := &corev1.Secret{}
258+
err = json.Unmarshal(out.Bytes(), got)
259+
if err != nil {
260+
t.Errorf("unexpected error: %v", err)
261+
}
262+
if !apiequality.Semantic.DeepEqual(got, test.expected) {
263+
t.Errorf("test %s\n expected:\n%#v\ngot:\n%#v", name, test.expected, got)
264+
}
265+
})
266+
}
267+
}

0 commit comments

Comments
 (0)