Skip to content

Commit b8ad65a

Browse files
committed
worked on
1 parent 2227919 commit b8ad65a

File tree

10 files changed

+266
-10
lines changed

10 files changed

+266
-10
lines changed

.circleci/update_website.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"gopkg.in/src-d/go-git.v4"
6+
"gopkg.in/src-d/go-git.v4/plumbing/object"
7+
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
8+
"gopkg.in/yaml.v2"
9+
"io/ioutil"
10+
"log"
11+
"os"
12+
"path/filepath"
13+
"strings"
14+
"time"
15+
)
16+
17+
var k8spatternsIoGitUrl = "https://github.com/k8spatterns/k8spatterns.io.git"
18+
19+
var patternTarget string
20+
var patternSource string
21+
22+
// Directory where examples are stored
23+
var websiteDir string
24+
25+
type example struct {
26+
Path string `yaml:"path"`
27+
}
28+
29+
type examplesList struct {
30+
Pattern string `yaml:"pattern"`
31+
Base string `yaml:"base"`
32+
Examples []example `yaml:"examples"`
33+
}
34+
35+
func init() {
36+
flag.StringVar(&patternTarget, "pattern-target", "examples", "target directory in site directory where to create pattern resources")
37+
flag.StringVar(&patternSource, "pattern-source", "", "source directory holding the pattern resources")
38+
}
39+
40+
func main() {
41+
42+
parseOptions()
43+
44+
// Creating working dir
45+
var err error
46+
websiteDir, err = ioutil.TempDir(".", "k8spatterns.io.")
47+
checkError(err)
48+
defer os.RemoveAll(websiteDir)
49+
50+
// Clone website
51+
log.Printf("* Cloning %s to %s\n", k8spatternsIoGitUrl, websiteDir)
52+
repo, err := git.PlainClone(websiteDir, false, &git.CloneOptions{
53+
URL: k8spatternsIoGitUrl,
54+
Depth: 1,
55+
})
56+
if err != nil {
57+
log.Fatalf("can't clone"+k8spatternsIoGitUrl+": %v", err)
58+
}
59+
workTree, err := repo.Worktree()
60+
checkError(err)
61+
62+
// Ensure target directory
63+
absTargetDir := websiteDir + "/" + patternTarget
64+
createDirIfNotExist(absTargetDir)
65+
66+
log.Printf("* Copying resource files from %s to %s", patternSource, absTargetDir)
67+
// Copy over examples files
68+
err = filepath.Walk(patternSource, getProcessIndexYmlFunc(absTargetDir, workTree))
69+
checkError(err)
70+
71+
// Check Git status
72+
status, err := workTree.Status()
73+
checkError(err)
74+
if len(status) == 0 {
75+
log.Println("* No changes detected")
76+
return
77+
}
78+
79+
log.Println()
80+
log.Println("* Changed/added files:")
81+
82+
for file := range status {
83+
log.Printf(" + %s == %c", file, status[file].Staging)
84+
}
85+
86+
// Git commit created files
87+
log.Printf("* Git committing changes")
88+
_, err = workTree.Commit("update_website: Update pattern resources files", &git.CommitOptions{
89+
Author: &object.Signature{
90+
Name: "Kubernetes Patterns - WebSite update",
91+
92+
When: time.Now(),
93+
},
94+
})
95+
checkError(err)
96+
97+
98+
// Git push (if env is set)
99+
gitToken := os.Getenv("GITHUB_TOKEN")
100+
if gitToken == "" {
101+
log.Println("* No GITHUB_TOKEN evn set --> no git push")
102+
return
103+
}
104+
105+
log.Printf("* Git push")
106+
err = repo.Push(&git.PushOptions{
107+
Auth: &http.BasicAuth{
108+
Username: "dummy",
109+
Password: gitToken,
110+
},
111+
})
112+
checkError(err)
113+
}
114+
115+
func parseOptions() {
116+
flag.Parse()
117+
118+
// Take parent dir by default
119+
if patternSource == "" {
120+
var err error
121+
patternSource, err = filepath.Abs("..")
122+
checkError(err)
123+
}
124+
125+
if filepath.IsAbs(patternTarget) {
126+
log.Fatal("pattern-target needs to be a relative path")
127+
}
128+
}
129+
130+
func getProcessIndexYmlFunc(targetDir string, worktree *git.Worktree) func(string, os.FileInfo, error) error {
131+
return func(path string, f os.FileInfo, e error) error {
132+
if f.IsDir() || f.Name() != "index.yml" {
133+
return nil
134+
}
135+
136+
examples := examplesList{}
137+
data, err := ioutil.ReadFile(path)
138+
checkError(err)
139+
140+
err = yaml.Unmarshal(data, &examples)
141+
checkError(err)
142+
143+
patternDir := strings.Replace(examples.Pattern, " ", "", -1)
144+
fullPatternDir := targetDir + "/" + patternDir
145+
createDirIfNotExist(fullPatternDir)
146+
147+
for _, example := range examples.Examples {
148+
dest := fullPatternDir + "/" + example.Path
149+
log.Printf(" - %s/%s", examples.Base, example.Path)
150+
copy(patternSource+"/"+examples.Base+"/"+example.Path, dest)
151+
_, err := worktree.Add(patternTarget + "/" + patternDir + "/" + example.Path)
152+
checkError(err)
153+
}
154+
return nil
155+
}
156+
}
157+
158+
func createDirIfNotExist(dir string) {
159+
_, err := os.Stat(dir)
160+
if os.IsNotExist(err) {
161+
checkError(os.Mkdir(dir, 0755))
162+
}
163+
}
164+
165+
func checkError(err error) {
166+
if err != nil {
167+
log.Fatal(err)
168+
}
169+
}
170+
171+
func copy(src string, dst string) {
172+
// Read all content of src to data
173+
data, err := ioutil.ReadFile(src)
174+
checkError(err)
175+
// Write data to dst
176+
err = ioutil.WriteFile(dst, data, 0644)
177+
checkError(err)
178+
}

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ link:behavorial/BatchJob/README.adoc[Batch Job]::
4141
link:behavorial/PeriodicJob/README.adoc[Periodic Job]::
4242
Reuses the link:behavorial/BatchJob/README.adoc[Batch Job] example, but runs it periodically at a configured schedule
4343
link:behavorial/DaemonService/README.adoc[Daemon Service]::
44-
Sample maintenance script for maintenance jobs on every node of a cluster [*]
44+
Sample maintenance script for maintenance jobs on every node of a cluster
4545
link:behavorial/SingletonService/README.adoc[Singleton Service]::
4646
Example of a PodDisruptionBudget for controlling voluntary disruptions [*]
4747
link:behavorial/SelfAwareness/README.adoc[Self Awareness]::

behavorial/BatchJob/README.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ This examples assumes a Kubernetes installation available.
77
In this case its best played through with Minikube as we need some support for PVs.
88
Check the link:../../INSTALL.adoc#minikube[INSTALL] documentation for how to install Minikube.
99

10-
1110
To access the PersistentVolume used in this demo, let's mount a local directory into the Minikube VM that we later then use a PersistentVolume mounted into the Pod:
1211

1312
[source, bash]

behavorial/DaemonService/README.adoc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
== Daemon Service
22

3-
IMPORTANT: The instructions have not been written/finished, but the resource file has been verified. Instructions will be added soon.
3+
In this example we learn how to create a _Daemon Service_ for allowing to run infrastructure-focused Pods on specific nodes.
4+
5+
This examples assumes a Kubernetes installation available.
6+
In this case its best played through with Minikube as we need some support for PVs.
7+
Check the link:../../INSTALL.adoc#minikube[INSTALL] documentation for how to install Minikube.
8+
9+
This example used our `random-generator` application for seeding `/dev/random` periodically by writing 100000 random number every time to `/dev/random`.
10+
11+
To be honest, this a make up example which should never be used for any application as the generated random number are not really random.
12+
13+
NOTE: You could achieve a similar effect with a _Periodic Job_ and and appropriate `nodeSelector`. However, a _Daemon Service_ is still more safe for such kind of a task, as it also works without changes also for nodes that will be added later.
14+
15+
Nevertheless, let's create a DaemonSet with
16+
17+
[source, bash]
18+
----
19+
kubectl create -f ./daemonset.yml
20+
----
21+
22+
You can now check the generates pods with
23+
24+
[source, bash]
25+
----
26+
kubectl get pods
27+
----
28+
29+
You see one Pod per node of your cluster.
30+
So for Minikube it should be a single pod and you can check the logs as ususal.
431

532

633
=== More Information

behavorial/DaemonService/daemonset.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ spec:
2121
- image: k8spatterns/random-generator:1.0
2222
name: random-generator
2323
# A somewhat useless command to demonstrate the concept running
24-
# on every node. This commands writer 10000 entries to /dev/random every 30s
24+
# on every node. This commands writes 10000 entries to /dev/random every 30s
2525
command:
2626
- sh
2727
- -c

behavorial/PeriodicJob/README.adoc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
== Periodic Job
22

3-
Our Cron Job example builds on top of the link:../BatchJob/README.adoc[Batch Job] example above, to the basic setuup is the same:
3+
Our Cron Job example builds on top of the link:../BatchJob/README.adoc[Batch Job] example above, to the basic setup is the same:
44

5-
* A Minikube installation avaiable, ideally with link:../../INSTALL.adoc#minikube[Minikube]
5+
* A Minikube installation available, ideally with link:../../INSTALL.adoc#minikube[Minikube]
66
* Minikube started with `minikube start --mount --mount-string="$(pwd)/logs:/example"` to mount the local `logs/` into the Minikube VM
77
8-
9-
Then create the PersistenVolume and PersistenVolumeClaim with
8+
Then create the PersistentVolume and PersistentVolumeClaim with
109

1110
[source, bash]
1211
----
1312
kubectl create -f pv-and-pvc.yml
1413
----
1514

16-
but you don't have to do it, if you already have your Minikube still running from the _Batch Job_example footnote:[If you reuse the Batch Job example, don't forget that the `random.log` is generated in the `logs/` directory over there.]
15+
but you don't have to do it, if you already have your Minikube still running from the _Batch Job_ example footnote:[If you reuse the Batch Job example, don't forget that the `random.log` is generated in the `logs/` directory over there.]
1716

1817
Now create the CronJob which fires every three minutes:
1918

foundational/AutomatedPlacement/index.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pattern: Automated Placement
2+
category: foundational
23
base: foundational/AutomatedPlacement
34
examples:
45
- description: Pod with node affinity

foundational/AutomatedPlacement/node-affinity.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Pod with node affinity
1+
# Pod with node affinity 2
22
---
33
apiVersion: v1
44
kind: Pod

go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module github.com/k8spatterns/examples
2+
3+
require (
4+
gopkg.in/src-d/go-git.v4 v4.10.0 // indirect
5+
gopkg.in/yaml.v2 v2.2.2 // indirect
6+
)

go.sum

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
2+
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/emirpasic/gods v1.9.0 h1:rUF4PuzEjMChMiNsVjdI+SyLu7rEqpQ5reNFnhC7oFo=
5+
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
6+
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
7+
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
8+
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
9+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
10+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
11+
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
12+
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e h1:RgQk53JHp/Cjunrr1WlsXSZpqXn+uREuHvUVcK82CV8=
13+
github.com/kevinburke/ssh_config v0.0.0-20180830205328-81db2a75821e/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
14+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
15+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
16+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
17+
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
18+
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
19+
github.com/pelletier/go-buffruneio v0.2.0 h1:U4t4R6YkofJ5xHm3dJzuRpPZ0mr5MMCoAWooScCR7aA=
20+
github.com/pelletier/go-buffruneio v0.2.0/go.mod h1:JkE26KsDizTr40EUHkXVtNPvgGtbSNq5BcowyYOWdKo=
21+
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
22+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
23+
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
24+
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
25+
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
26+
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
27+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
28+
github.com/xanzy/ssh-agent v0.2.0 h1:Adglfbi5p9Z0BmK2oKU9nTG+zKfniSfnaMYB+ULd+Ro=
29+
github.com/xanzy/ssh-agent v0.2.0/go.mod h1:0NyE30eGUDliuLEHJgYte/zncp2zdTStcOnWhgSqHD8=
30+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
31+
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
32+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA=
33+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
34+
golang.org/x/sys v0.0.0-20180903190138-2b024373dcd9/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
35+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
36+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/src-d/go-billy.v4 v4.2.1 h1:omN5CrMrMcQ+4I8bJ0wEhOBPanIRWzFC953IiXKdYzo=
39+
gopkg.in/src-d/go-billy.v4 v4.2.1/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
40+
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
41+
gopkg.in/src-d/go-git.v4 v4.10.0 h1:NWjTJTQnk8UpIGlssuefyDZ6JruEjo5s88vm88uASbw=
42+
gopkg.in/src-d/go-git.v4 v4.10.0/go.mod h1:Vtut8izDyrM8BUVQnzJ+YvmNcem2J89EmfZYCkLokZk=
43+
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
44+
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
45+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
46+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)