1

I'm struggling to create two JSON config files in the main application directory (/publish) while leaving all other files in that directory intact. I tried the following:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: url_to_image
        ports:
        - containerPort: 1234
        volumeMounts:
        - name: config-files
          mountPath: "/publish"
      volumes:
      - name: config-files
        configMap:
          name: myapp
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  ports:
    - port: 80
      targetPort: 1234
      protocol: TCP
  type: NodePort
  selector:
    app: myapp
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: myapp
  labels:
    app: myapp
data:
  appsettings.json: |
    { some JSON }
  sharedsettings.json: |
    { more JSON }

Two config files (appsettings.json and sharedsettings.json) are correctly created but unfortunately all other files in /publish are deleted in the process.

What would be the best approach?

chrisvdb
  • 1,199
  • 2
  • 10
  • 15

1 Answers1

1

Ok, found a solution but it's not very elegant. Any better solutions out there?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: url_to_image
        ports:
        - containerPort: 1234
        volumeMounts:
        - name: appsettings
          mountPath: "/publish/appsettings.json"
          subPath: whatever
        volumeMounts:
        - name: sharedsettings
          mountPath: "/publish/sharedsettings.json"
          subPath: whatever
      volumes:
      - name: appsettings
        configMap:
          name: myapp
          items:
          - key: appsettings.json
            path: whatever
      - name: sharedsettings
        configMap:
          name: myapp
          items:
          - key: sharedsettings.json
            path: whatever
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  ports:
    - port: 80
      targetPort: 1234
      protocol: TCP
  type: NodePort
  selector:
    app: myapp
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: myapp
  labels:
    app: myapp
data:
  appsettings.json: |
    { some JSON }
  sharedsettings.json: |
    { more JSON }
chrisvdb
  • 1,199
  • 2
  • 10
  • 15
  • Hi @chrisvdb, why do you claim this solution is not elegant ? For me it looks very elegant, and moreover is "Kubertnetisch" and avoiding hacky approach (initContainer sharing with main Container the same PV) – Nepomucen Mar 10 '21 at 12:04
  • @Nepomucen it's been a while... but I think I didn't like the fake 'whatever' path! – chrisvdb Mar 10 '21 at 12:47