Browse Source

Initial push

Fred Damstra (k8s1) 2 years ago
parent
commit
1ad4b73f8e
3 changed files with 153 additions and 0 deletions
  1. 6 0
      Dockerfile
  2. 61 0
      kaniko-update-route53.yaml
  3. 86 0
      update-route53.sh

+ 6 - 0
Dockerfile

@@ -0,0 +1,6 @@
+FROM ubuntu
+
+COPY . /opt/update_route53
+RUN chmod 755 /opt/update_route53/update_route53.sh
+
+ENTRYPOINT ["/opt/update_route53/update_route53.sh"]

+ 61 - 0
kaniko-update-route53.yaml

@@ -0,0 +1,61 @@
+---
+# Kaniko builds images without docker
+#
+# Use 'kubectl create -f kaniko.yaml' to execute
+# 
+# NOTES:
+# If this is your first kaniko, you need to add a token:
+# In gogs: User->Settings->Applications
+# kubectl create secret generic git-token --from-literal='GIT_TOKEN=<your-token>'
+# 
+# You need to add credentials to the docker registry:
+# kubectl create secret docker-registry  docker-regcred \
+#  --docker-server=https://index.docker.io/v1/ \
+#  --docker-username=<your-username> \
+#  --docker-password=<your-password>
+apiVersion: v1
+kind: Job
+metadata:
+  name: kaniko-update-route53
+spec:
+  template:
+    ttlSecondsAfterFinished: 100
+    backoffLimit: 4
+    containers:
+    - name: kaniko-update-route53
+      image: gcr.io/kaniko-project/executor:v1.9.1 
+      args:
+      - "--dockerfile=./Dockerfile"
+      - "--context=git://git.monkeybox.org/Containers/update-route53#refs/heads/master"
+      - "--destination=fdamstra/update-route53:latest"
+      env:
+        - name: GIT_TOKEN
+          valueFrom:
+            secretKeyRef:
+              name: git-token
+              key: GIT_TOKEN
+      volumeMounts:
+      - name: docker-config
+        mountPath: /kaniko/.docker/
+    dnsPolicy: "None"
+    dnsConfig:
+      nameservers:
+        - 10.42.42.239
+        - 10.42.42.1
+      searches:
+        - default.svc.cluster.local
+        - svc.cluster.local
+        - cluster.local
+      options:
+        - name: ndots
+          value: "2"
+        - name: edns0
+        - name: trust-ad
+    restartPolicy: Never
+    volumes:
+    - name: docker-config
+      secret:
+        secretName: docker-regcred
+        items:
+            - key: .dockerconfigjson
+              path: config.json

+ 86 - 0
update-route53.sh

@@ -0,0 +1,86 @@
+#!/bin/bash
+ 
+# More advanced options below
+# The Time-To-Live of this recordset
+TTL=300
+# Change this if you want
+COMMENT="Auto updating @ `date`"
+# Change to AAAA if using an IPv6 address
+TYPE="A"
+ 
+# Get the external IP address
+IP=`curl -ss http://ipv4.icanhazip.com/`
+ 
+function valid_ip()
+{
+    local  ip=$1
+    local  stat=1
+ 
+    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
+        OIFS=$IFS
+        IFS='.'
+        ip=($ip)
+        IFS=$OIFS
+        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
+            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
+        stat=$?
+    fi
+    return $stat
+}
+
+DIR=/scratch/
+LOGFILE="$DIR/update-route53.log"
+IPFILE="$DIR/update-route53.ip"
+ 
+if ! valid_ip $IP; then
+    echo "Invalid IP address: $IP" >> "$LOGFILE"
+    exit 1
+fi
+ 
+# Check if the IP has changed
+if [ ! -f "$IPFILE" ]
+    then
+    touch "$IPFILE"
+fi
+ 
+if grep -Fxq "$IP" "$IPFILE"; then
+    # code if found
+    echo "IP is still $IP. Exiting" >> "$LOGFILE"
+    exit 0
+else
+    echo "IP has changed to $IP" >> "$LOGFILE"
+    # Fill a temp file with valid JSON
+    TMPFILE=$(mktemp /tmp/temporary-file.XXXXXXXX)
+    cat > ${TMPFILE} << EOF
+    {
+      "Comment":"$COMMENT",
+      "Changes":[
+        {
+          "Action":"UPSERT",
+          "ResourceRecordSet":{
+            "ResourceRecords":[
+              {
+                "Value":"$IP"
+              }
+            ],
+            "Name":"$RECORDSET",
+            "Type":"$TYPE",
+            "TTL":$TTL
+          }
+        }
+      ]
+    }
+EOF
+ 
+    # Update the Hosted Zone record
+    aws route53 change-resource-record-sets \
+        --hosted-zone-id $ZONEID \
+        --change-batch file://"$TMPFILE" >> "$LOGFILE"
+    echo "" >> "$LOGFILE"
+ 
+    # Clean up
+    rm $TMPFILE
+fi
+ 
+# All Done - cache the IP address for next time
+echo "$IP" > "$IPFILE"