kpt icon indicating copy to clipboard operation
kpt copied to clipboard

Duplicated resources with the same kind, group, namespace and name don't report an error.

Open patrickchense opened this issue 5 years ago • 1 comments

Expected behavior

If we accidentally apply two resources with the same kind, group, namespace, and name, kpt should report an error.

Actual behavior

No error. And dry-run and apply return different results.

Information

kpt version: 0.36.1

Two PubSubTopics with the same kind, group, namespace and name pass preview and apply.

apiVersion: pubsub.cnrm.cloud.google.com/v1beta1
kind: PubSubTopic
metadata:
  labels:
    label-one: foobar
  name: foobar-pubsub
  namespace: test
---
apiVersion: pubsub.cnrm.cloud.google.com/v1beta1
kind: PubSubTopic
metadata:
  labels:
    label-one: foobar
  name: foobar-pubsub
  namespace: test

kpt live preview Output:

pubsubtopic.pubsub.cnrm.cloud.google.com/foobar-pubsub, created (preview)
pubsubtopic.pubsub.cnrm.cloud.google.com/foobar-pubsub, created (preview)
2 resource(s) applied. 2 created, 0 unchanged, 0 configured (preview)
0 resource(s) pruned, 0 skipped (preview)

kpt live apply Output:

pubsubtopic.pubsub.cnrm.cloud.google.com/foobar-pubsub created
pubsubtopic.pubsub.cnrm.cloud.google.com/foobar-pubsub unchanged
2 resource(s) applied. 1 created, 1 unchanged, 0 configured
0 resource(s) pruned, 0 skipped

Only one resource applied eventually.

I'm not sure this is the expected behavior of the kpt preview/apply. But this can be caused by some typo or copy/paste mistake, and users should be notified or warned.

patrickchense avatar Mar 18 '21 10:03 patrickchense

Thanks for reporting this @PatrickChenSe . The overall behavior is in line with how kubectl behaves. I am not sure we would take on fixing this issue in a client side tool. Here is an example:

 ~/src/sandbox/gh1562 » kubectl apply -f combined_config.yml --dry-run=client
deployment.apps/mysql created (dry run)
service/mysql created (dry run)
service/mysql created (dry run)
 ~/src/sandbox/gh1562 » kubectl apply -f combined_config.yml 
deployment.apps/mysql created
service/mysql created
service/mysql unchanged
 ~/src/sandbox/gh1562 » kubectl get all
NAME                        READY   STATUS              RESTARTS   AGE
pod/mysql-8b44f9fdc-d8snd   0/1     ContainerCreating   0          18s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP    13m
service/mysql        ClusterIP   None         <none>        3306/TCP   18s

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mysql   0/1     1            0           18s

NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/mysql-8b44f9fdc   1         1         0       18s

The combined config has duplicates:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD # Use secret in real usage
          value: changeme! # {"$kpt-set":"mysqlpw"}
        ports:
        - containerPort: 3306
          name: mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
    - port: 3306
  selector:
    app: mysql
  clusterIP: None

mikebz avatar Mar 25 '21 16:03 mikebz