Skip to content

Commit c8b32b1

Browse files
committed
fix: allow container name & image selector together
1 parent 68d01e9 commit c8b32b1

File tree

3 files changed

+8
-47
lines changed

3 files changed

+8
-47
lines changed

pkg/devspace/config/loader/validate.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,6 @@ func validateDev(config *latest.Config) error {
385385
if config.Dev.Ports != nil {
386386
for index, port := range config.Dev.Ports {
387387
// Validate imageName and label selector
388-
if port.ContainerName != "" && len(port.LabelSelector) == 0 {
389-
return errors.Errorf("Error in config: containerName is defined but label selector is nil in ports config at index %d", index)
390-
}
391-
392388
if len(port.LabelSelector) == 0 && port.ImageSelector == "" {
393389
return errors.Errorf("Error in config: image selector and label selector are nil in ports config at index %d", index)
394390
}
@@ -405,10 +401,6 @@ func validateDev(config *latest.Config) error {
405401
if config.Dev.Sync != nil {
406402
for index, sync := range config.Dev.Sync {
407403
// Validate imageName and label selector
408-
if sync.ContainerName != "" && len(sync.LabelSelector) == 0 {
409-
return errors.Errorf("Error in config: containerName is defined but label selector is nil in sync config at index %d", index)
410-
}
411-
412404
if len(sync.LabelSelector) == 0 && sync.ImageSelector == "" {
413405
return errors.Errorf("Error in config: image selector and label selector are nil in sync config at index %d", index)
414406
}
@@ -443,9 +435,6 @@ func validateDev(config *latest.Config) error {
443435
if selector.ImageSelector != "" && len(selector.LabelSelector) > 0 {
444436
return errors.Errorf("Error in config: dev.logs.selectors[%d].imageSelector and dev.logs.selectors[%d].labelSelector cannot be used together", index, index)
445437
}
446-
if selector.ImageSelector != "" && selector.ContainerName != "" {
447-
return errors.Errorf("Error in config: dev.logs.selectors[%d].imageSelector and dev.logs.selectors[%d].containerName cannot be used together", index, index)
448-
}
449438
}
450439
}
451440

pkg/devspace/config/loader/validate_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,6 @@ func TestValidateDev(t *testing.T) {
9696
err := validateDev(config)
9797
assert.NilError(t, err)
9898

99-
config = &latest.Config{
100-
Dev: latest.DevConfig{
101-
Ports: []*latest.PortForwardingConfig{
102-
{
103-
ContainerName: "fakeContainer",
104-
Name: "someName",
105-
PortMappings: []*latest.PortMapping{
106-
{
107-
LocalPort: &localPort,
108-
RemotePort: &remotePort,
109-
},
110-
},
111-
},
112-
},
113-
},
114-
}
115-
116-
err = validateDev(config)
117-
assert.Error(t, err, "Error in config: containerName is defined but label selector is nil in ports config at index 0")
118-
11999
// test sync
120100
config = &latest.Config{
121101
Dev: latest.DevConfig{
@@ -135,20 +115,6 @@ func TestValidateDev(t *testing.T) {
135115
err = validateDev(config)
136116
assert.NilError(t, err)
137117

138-
config = &latest.Config{
139-
Dev: latest.DevConfig{
140-
Sync: []*latest.SyncConfig{
141-
{
142-
ContainerName: "fakeContainer",
143-
Name: "someName",
144-
},
145-
},
146-
},
147-
}
148-
149-
err = validateDev(config)
150-
assert.Error(t, err, "Error in config: containerName is defined but label selector is nil in sync config at index 0")
151-
152118
// test replace pods
153119
config = &latest.Config{
154120
Dev: latest.DevConfig{

pkg/devspace/kubectl/selector/selector.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (f *filter) SelectContainers(ctx context.Context, selectors ...Selector) ([
147147
retList = append(retList, containersByLabelSelector...)
148148
}
149149

150-
containersByImage, err := byImageName(ctx, f.client, namespace, s.ImageSelector, s.FilterContainer, s.SkipInitContainers)
150+
containersByImage, err := byImageName(ctx, f.client, namespace, s.ImageSelector, s.ContainerName, s.FilterContainer, s.SkipInitContainers)
151151
if err != nil {
152152
return nil, errors.Wrap(err, "pods by image name")
153153
}
@@ -310,7 +310,7 @@ func byLabelSelector(ctx context.Context, client kubectl.Client, namespace strin
310310
return retPods, nil
311311
}
312312

313-
func byImageName(ctx context.Context, client kubectl.Client, namespace string, imageSelector []string, skipContainer FilterContainer, skipInit bool) ([]*SelectedPodContainer, error) {
313+
func byImageName(ctx context.Context, client kubectl.Client, namespace string, imageSelector []string, containerName string, skipContainer FilterContainer, skipInit bool) ([]*SelectedPodContainer, error) {
314314
retPods := []*SelectedPodContainer{}
315315
if len(imageSelector) > 0 {
316316
podList, err := client.KubeClient().CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
@@ -325,6 +325,9 @@ func byImageName(ctx context.Context, client kubectl.Client, namespace string, i
325325
if skipContainer != nil && skipContainer(&pod, &container) {
326326
continue
327327
}
328+
if containerName != "" && container.Name != containerName {
329+
continue
330+
}
328331

329332
if imageselector.CompareImageNames(imageName, container.Image) {
330333
retPod := pod
@@ -342,6 +345,9 @@ func byImageName(ctx context.Context, client kubectl.Client, namespace string, i
342345
if skipContainer != nil && skipContainer(&pod, &container) {
343346
continue
344347
}
348+
if containerName != "" && container.Name != containerName {
349+
continue
350+
}
345351

346352
// check if it is a replaced pod and if yes, check if the imageName and container name matches
347353
if pod.Labels != nil && pod.Labels[ReplacedLabel] == "true" && pod.Annotations != nil && pod.Annotations[MatchedContainerAnnotation] == container.Name {

0 commit comments

Comments
 (0)