Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add materials for chapters 9 and 10 #3

Merged
merged 4 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Chapter09/example_sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
docker.io:
tls-verify: true
images:
alpine: []
nginx:
- "latest"
images-by-tag-regex:
httpd: ^2\.4\.[0-9]*-alpine$
quay.io:
tls-verify: true
images:
fedora/fedora:
- latest
registry.access.redhat.com:
tls-verify: true
images:
ubi8:
- "8.4"
- "8.5"
9 changes: 9 additions & 0 deletions Chapter09/kube_sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
k8s.gcr.io:
tls-verify: true
images-by-tag-regex:
kube-apiserver: ^v1\.22\..*
kube-controller-manager: ^v1\.22\..*
kube-proxy: ^v1\.22\..*
kube-scheduler: ^v1\.22\..*
coredns/coredns: ^v1\.8\..*
etcd: 3\.4.[0-9]*-[0-9]*
27 changes: 27 additions & 0 deletions Chapter09/local_registry/customizations/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 0.1
log:
fields:
service: registry
storage:
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
delete:
enabled: true
auth:
htpasswd:
realm: basic-realm
path: /var/lib/htpasswd
http:
addr: :5000
headers:
X-Content-Type-Options: [nosniff]
tls:
certificate: /etc/pki/certs/tls.crt
key: /etc/pki/certs/tls.key
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
5 changes: 5 additions & 0 deletions Chapter09/local_registry/minimal_httpd/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM registry.access.redhat.com/ubi8:latest
RUN dnf install -y httpd && dnf clean all -y
COPY index.html /var/www/html
RUN dnf install -y git && dnf clean all -y
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]
1 change: 1 addition & 0 deletions Chapter09/local_registry/minimal_httpd/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
4 changes: 4 additions & 0 deletions Chapter09/openshift_sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
quay.io:
tls-verify: true
images-by-tag-regex:
openshift-release-dev/ocp-release: ^4\.9\..*-x86_64$
11 changes: 11 additions & 0 deletions Chapter10/FROM_auth_error/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM local-registry.example.com/ubi8

# Update image and install httpd
RUN yum install -y httpd && yum clean all -y

# Expose the default httpd port 80
EXPOSE 80

# Run the httpd
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]

11 changes: 11 additions & 0 deletions Chapter10/FROM_repo_not_found/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM registry.access.redhat.com/ubi_8

# Update image and install httpd
RUN yum install -y httpd && yum clean all -y

# Expose the default httpd port 80
EXPOSE 80

# Run the httpd
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]

11 changes: 11 additions & 0 deletions Chapter10/FROM_tag_not_found/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM docker.io/library/fedora:sometag

# Update image and install httpd
RUN dnf install -y httpd && dnf clean all -y

# Expose the default httpd port 80
EXPOSE 80

# Run the httpd
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]

11 changes: 11 additions & 0 deletions Chapter10/RUN_command_error/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM registry.access.redhat.com/ubi8

# Update image and install httpd
RUN yum install -y htpd && yum clean all -y

# Expose the default httpd port 80
EXPOSE 80

# Run the httpd
CMD ["/usr/sbin/httpd", "-DFOREGROUND"]

11 changes: 11 additions & 0 deletions Chapter10/buildah_script_example/custom-httpd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -euo pipefail

# Trying to pull a non-existing tag of Fedora official image
container=$(buildah from docker.io/library/fedora:non-existing-tag)
buildah run $container -- dnf install -y httpd; dnf clean all -y
buildah config --cmd "httpd -DFOREGROUND" $container
buildah config --port 80 $container
buildah commit $container myhttpd
buildah tag custom-httpd registry.example.com/custom-httpd:v0.0.1
24 changes: 24 additions & 0 deletions Chapter10/students/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM docker.io/library/golang AS builder

# Copy files for build
RUN mkdir -p /go/src/students/models
COPY go.mod main.go /go/src/students
COPY models/main.go /go/src/students/models

# Set the working directory
WORKDIR /go/src/students

# Download dependencies
RUN go get -d -v ./...

# Install the package
RUN go build -v

# Runtime image
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest as bin
COPY --from=builder /go/src/students /usr/local/bin
COPY entrypoint.sh /

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]
14 changes: 14 additions & 0 deletions Chapter10/students/db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE students (
id integer NOT NULL,
firstname char(256) NOT NULL,
middlename char(256) NOT NULL,
lastname char(256) NOT NULL,
class char(128) NOT NULL,
course char(128) NOT NULL);

INSERT INTO students (id, firstname, middlename, lastname, class, course) VALUES
('10149', 'Frank', 'Vincent', 'Zappa', '3A', 'Composition');

ALTER TABLE students ADD PRIMARY KEY (id);


9 changes: 9 additions & 0 deletions Chapter10/students/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -euo pipefail

if [ $# -eq 0 ]; then
exec /usr/local/bin/students
else
exec $@
fi
5 changes: 5 additions & 0 deletions Chapter10/students/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/PacktPublishing/Podman-for-DevOps/Chapter10/students

go 1.16

require github.com/lib/pq v1.10.4 // indirect
2 changes: 2 additions & 0 deletions Chapter10/students/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
71 changes: 71 additions & 0 deletions Chapter10/students/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"

"github.com/PacktPublishing/Podman-for-DevOps/Chapter10/students/models"

_ "github.com/lib/pq"
)

func main() {

var (
username string
password string
host string
port string
database string
usage string
)

flag.StringVar(&username, "username", "admin", "Default database username")
flag.StringVar(&password, "password", "password", "Default database password")
flag.StringVar(&host, "host", "localhost", "Default host running the database")
flag.StringVar(&port, "port", "5432", "Default database port")
flag.StringVar(&database, "database", "students", "Default application database")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), usage, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()

connString := "postgres://" + username + ":" + password + "@" + host + ":" + port + "/" + database + "?sslmode=disable"
log.Printf("Connecting to host %s:%s, database %s", host, port, database)

var err error
models.DB, err = sql.Open("postgres", connString)
if err != nil {
log.Fatal(err)
}

http.HandleFunc("/students", getStudents)

if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}

}

// getStudents is a HTTP GET method to print a full list of the students in JSON encoding
func getStudents(w http.ResponseWriter, r *http.Request) {
studentsInfo, err := models.GetStudents()
if err != nil {
http.Error(w, http.StatusText(500), 500)
return
}

for _, std := range studentsInfo {
s, err := json.Marshal(std)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, "%s", string(s))
}
}
53 changes: 53 additions & 0 deletions Chapter10/students/models/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package models

import (
"database/sql"
"strings"
)

var DB *sql.DB

type Student struct {
Id int
FirstName string
MiddleName string
LastName string
Class string
Course string
}

func GetStudents() ([]Student, error) {
rows, err := DB.Query("SELECT * from students")
if err != nil {
return nil, err
}

defer rows.Close()

var students []Student

for rows.Next() {
var student Student
err := rows.Scan(&student.Id, &student.FirstName, &student.MiddleName, &student.LastName, &student.Class, &student.Course)
if err != nil {
return nil, err
}

// Trim uncessary spaces from string fields
student.FirstName = strings.TrimSpace(student.FirstName)
student.MiddleName = strings.TrimSpace(student.MiddleName)
student.LastName = strings.TrimSpace(student.LastName)
student.Class = strings.TrimSpace(student.Class)
student.Course = strings.TrimSpace(student.Course)

// Append the struct to the slice of students
students = append(students, student)
}

err = rows.Err()
if err != nil {
return nil, err
}

return students, nil
}