apiVersion: v1
# Nice thing about Namespace usage: we can just delete the entire set of things around a deployment,
# if they're in a namespace by deleting the Namespace.
# Because this is based on a very old project of mine (From 2011/2012), I have been putting it into a namespace called "legacy-sprint" to differentiate from local development I've done.
# That should be updatable with no conflicts as long as it's updated everywhere it's used.
kind: Namespace 
metadata:
  name: legacy-sprint
  labels:
    name: legacy-sprint
---
apiVersion: v1
kind: Secret
metadata:
   name: db-creds
   namespace: legacy-sprint
type: kubernetes.io/basic-auth
data:
   username: cm9vdA==
   password: cXdlcnR5dWlvcA==
---
# Okay, this is a bit unorthodox.
# In the original (And current, redundantly) version of the code, we actually just reinitialize the db if there is no db tables, but we have a connection to an sql database.
# Instead, I want to encode the sql scripts into the ConfigMap here, so that we can execute them.
# This way, I don't need to ensure that the person applying this script also happens to have my sql startup scripts.
# Just to have the SQL deployment automatically populate the base fields.
# Base64 encoded just to save space - this file will be getting long as is.
# One note - these files create a database name `iat_352_project_3_alexander_ryan` - that is important to the codebase, although later versions may make a config value to pull that from.
apiVersion: v1
kind: Secret
metadata:
  name: db-create-scripts
  namespace: legacy-sprint
data:
  createTablesScripts.sql: Q1JFQVRFIERBVEFCQVNFIGlhdF8zNTJfcHJvamVjdF8zX2FsZXhhbmRlcl9yeWFuOwoKQ1JFQVRFIFRBQkxFIGlhdF8zNTJfcHJvamVjdF8zX2FsZXhhbmRlcl9yeWFuLmxpbmVzX3RhYmxlICgKICBgSURgIGludCgxMSkgTk9UIE5VTEwsCiAgYExJTkVgIHZhcmNoYXIoNDUpIERFRkFVTFQgTlVMTCwKICBQUklNQVJZIEtFWSAoYElEYCkKKTsKCkNSRUFURSBUQUJMRSBpYXRfMzUyX3Byb2plY3RfM19hbGV4YW5kZXJfcnlhbi5sb2NhdGlvbnMgKAogIGBJRGAgaW50KDExKSBOT1QgTlVMTCBBVVRPX0lOQ1JFTUVOVCwKICBgVElUTEVgIHZhcmNoYXIoMjU1KSBOT1QgTlVMTCwKICBgREVTQ1JJUFRJT05gIHZhcmNo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maWMuJywzLDEsNDkuMjI1Njc3LC0xMjMuMDAzODAzKTsK
---
apiVersion: v1
kind: Secret
metadata:
   name: api-keys
   namespace: legacy-sprint
   #Provide your own Google Maps API Key here as .gMapAPIKey # UHJvdmlkZSB5b3VyIG93biBHb29nbGUgTWFwcyBBUEkgS2V5IGhlcmU= 
   #Provide your own Flickr API Key here as .flickrAPIKey # UHJvdmlkZSB5b3VyIG93biBGbGlja3IgQVBJIEtleSBoZXJl
data:
   .gMapAPIKey: UHJvdmlkZSB5b3VyIG93biBHb29nbGUgTWFwcyBBUEkgS2V5IGhlcmU=
   .flickrAPIKey: UHJvdmlkZSB5b3VyIG93biBGbGlja3IgQVBJIEtleSBoZXJl
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: legacy-sprint-db
  namespace: legacy-sprint
  labels:
    app: legacy-sprint-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: legacy-sprint-db
  template:
    metadata:
      labels:
        app: legacy-sprint-db
    spec:
      containers:
        - name: mysql-db
          image: cimg/mysql:5.7.38
          # Due to initial connection conditions, as I understand, to connect to this db locally, I do not have SSL enabled, so I have connected to it via the following link:
          # Connection via DB Viewer: jdbc:mysql://localhost:<containerPort>/?allowPublicKeyRetrieval=true&useSSL=false
          # (Could use SSL hardening on this and the main server, if configured correctly.)
          ports:
          - containerPort: 3306
            name: db-port
          volumeMounts:
            - name: db-creds
              readOnly: true
              mountPath: "/etc/secret-volume"
            - name: db-scripts
              readOnly: true
              mountPath: "/docker-entrypoint-initdb.d"
          env:
          - name: MYSQL_ROOT_PASSWORD_FILE
            value: "/etc/secret-volume/password"
          #- name: MYSQL_USER_FILE
          #  value: "/etc/secret-volume/username"
          #- name: MYSQL_PASSWORD_FILE
          #  value: "/etc/secret-volume/password"
      volumes:
        - name: db-creds
          secret:
            secretName: db-creds
        - name: db-scripts
          secret:
            secretName: db-create-scripts
          #configMap:
            #name: db-create-scripts
            #items:
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: legacy-sprint-main
  namespace: legacy-sprint
  labels:
    app: legacy-sprint-main
spec:
  replicas: +1
  selector:
    matchLabels:
      app: legacy-sprint-main
  template:
    metadata:
      labels:
        app: legacy-sprint-main
    spec:
      containers:
        - name: main-server
          image: mt1st/flickr-api-project:latest
          #images: legacy-sprint:testing / mt1st/flickr-api-project:needs-maps-fix / mt1st/flickr-api-project:possible-google-maps-version
          ports:
          - containerPort: 8080
            name: server-port
          volumeMounts:
            - name: secret-value-volume
              readOnly: true
              mountPath: "/etc/secret-volume"
            - name: config-volume
              #readOnly: true
              mountPath: "/config"
          env:
          - name: envValue
            value: "environmentSnake"
      volumes:
        - name: secret-value-volume
          projected:
            sources:
            - secret:
                name: db-creds
            - secret:
                name: api-keys
        - name: config-volume
          configMap:
            # Provide the name of the ConfigMap you want to mount.
            name: db-config
            # An array of keys from the ConfigMap to create as files
            # When left blank as they are now, that means they will be key as filename, and value as contents.
            items:
---

apiVersion: v1
kind: Service
metadata:
  name: mysql-db-service
  namespace: legacy-sprint
  labels:
    app: legacy-sprint-db
spec:
  ports:
  - name: mysql-db-port
    protocol: TCP
    port: 3306
    targetPort: db-port # The target port - as I understand, relevant if we want to only allow specific ports to be accessed, as in this case.
    nodePort: 30306
  selector:
    app: legacy-sprint-db
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  name: main-server-service
  namespace: legacy-sprint
  labels:
    app: legacy-sprint-main
spec:
  ports:
  - name: legacy-main-port
    protocol: TCP
    port: 8084 
    targetPort: server-port # The target port - as I understand, relevant if we want to only allow specific ports to be accessed, as in this case.
    #nodePort: 30000
  selector:
    app: legacy-sprint-main
  type: LoadBalancer
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: db-config
  namespace: legacy-sprint
data:  
  dbPort: "3306"
  dbHost: "mysql-db-service.legacy-sprint"

# Because mysql:8.0.42-oraclelinux9 doesn't support mysql-connector-java 5.1.2, nor 6.0.6, we're trying another MySQL server with...older support.
# This all because openjdk 11 (JRE 7?)is being used in the main server image.