@@ -82,36 +82,58 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
8282
8383 for _ , v := range args {
8484 if strings .HasPrefix (v , "secret/" ) {
85- // format secret/namespace-name/secret-name
85+ // format secret/namespace-name/secret-name[/data-key]
8686 pathParts := strings .Split (v , "/" )
87- if len (pathParts ) != 3 {
88- return nil , types .NewExitCodeError (constants .EXIT_CODE_SPEC_ISSUES , errors .Errorf ("secret path %q must have 3 components" , v ))
87+ if len (pathParts ) > 4 {
88+ return nil , types .NewExitCodeError (constants .EXIT_CODE_SPEC_ISSUES , errors .Errorf ("secret path %s must have at most 4 components" , v ))
89+ }
90+ if len (pathParts ) < 3 {
91+ return nil , types .NewExitCodeError (constants .EXIT_CODE_SPEC_ISSUES , errors .Errorf ("secret path %s must have at least 3 components" , v ))
8992 }
9093
9194 data , err := LoadFromSecret (ctx , client , pathParts [1 ], pathParts [2 ])
9295 if err != nil {
9396 return nil , types .NewExitCodeError (constants .EXIT_CODE_SPEC_ISSUES , errors .Wrap (err , "failed to get spec from secret" ))
9497 }
9598
96- // Append all data in the secret. Some may not be specs, but that's ok. They will be ignored.
97- for _ , spec := range data {
98- rawSpecs = append (rawSpecs , string (spec ))
99+ // If we have a key defined, then load specs from that key only.
100+ if len (pathParts ) == 4 {
101+ spec , ok := data [pathParts [3 ]]
102+ if ok {
103+ rawSpecs = append (rawSpecs , string (spec ))
104+ }
105+ } else {
106+ // Append all data in the secret. Some may not be specs, but that's ok. They will be ignored.
107+ for _ , spec := range data {
108+ rawSpecs = append (rawSpecs , string (spec ))
109+ }
99110 }
100111 } else if strings .HasPrefix (v , "configmap/" ) {
101- // format configmap/namespace-name/configmap-name
112+ // format configmap/namespace-name/configmap-name[/data-key]
102113 pathParts := strings .Split (v , "/" )
103- if len (pathParts ) != 3 {
104- return nil , errors .Errorf ("configmap path %q must have 3 components" , v )
114+ if len (pathParts ) > 4 {
115+ return nil , types .NewExitCodeError (constants .EXIT_CODE_SPEC_ISSUES , errors .Errorf ("configmap path %s must have at most 4 components" , v ))
116+ }
117+ if len (pathParts ) < 3 {
118+ return nil , types .NewExitCodeError (constants .EXIT_CODE_SPEC_ISSUES , errors .Errorf ("configmap path %s must have at least 3 components" , v ))
105119 }
106120
107121 data , err := LoadFromConfigMap (ctx , client , pathParts [1 ], pathParts [2 ])
108122 if err != nil {
109- return nil , errors .Wrap (err , "failed to get spec from configmap" )
123+ return nil , types . NewExitCodeError ( constants . EXIT_CODE_SPEC_ISSUES , errors .Wrap (err , "failed to get spec from configmap" ) )
110124 }
111125
112- // Append all data in the configmap. Some may not be specs, but that's ok. They will be ignored.
113- for _ , spec := range data {
114- rawSpecs = append (rawSpecs , spec )
126+ // If we have a key defined, then load specs from that key only.
127+ if len (pathParts ) == 4 {
128+ spec , ok := data [pathParts [3 ]]
129+ if ok {
130+ rawSpecs = append (rawSpecs , spec )
131+ }
132+ } else {
133+ // Append all data in the configmap. Some may not be specs, but that's ok. They will be ignored.
134+ for _ , spec := range data {
135+ rawSpecs = append (rawSpecs , spec )
136+ }
115137 }
116138 } else if _ , err := os .Stat (v ); err == nil {
117139 b , err := os .ReadFile (v )
0 commit comments