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

hack update: generate unit tests for new k8rs version #9708

Merged
merged 3 commits into from
Nov 17, 2020
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
55 changes: 38 additions & 17 deletions hack/update/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,58 @@ limitations under the License.
package update

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"k8s.io/klog/v2"
)

// fsUpdate updates local filesystem repo files according to the given schema and data.
// Returns if the update actually changed anything, and any error occurred.
func fsUpdate(fsRoot string, schema map[string]Item, data interface{}) (changed bool, err error) {
var mode os.FileMode = 0644
for path, item := range schema {
path = filepath.Join(fsRoot, path)
blob, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
info, err := os.Stat(path)
if err != nil {
return false, err
// if the item's content is already set, give it precedence over any current file content
var content []byte
if item.Content == nil {
info, err := os.Stat(path)
if err != nil {
return false, fmt.Errorf("unable to get file content: %w", err)
}
mode = info.Mode()
content, err = ioutil.ReadFile(path)
if err != nil {
return false, fmt.Errorf("unable to read file content: %w", err)
}
item.Content = content
}
mode := info.Mode()

item.Content = blob
chg, err := item.apply(data)
if err != nil {
return false, err
if err := item.apply(data); err != nil {
return false, fmt.Errorf("unable to update file: %w", err)
}
if chg {
if !bytes.Equal(content, item.Content) {
// make sure path exists
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return false, fmt.Errorf("unable to create directory: %w", err)
}
if err := ioutil.WriteFile(path, item.Content, mode); err != nil {
return false, fmt.Errorf("unable to write file: %w", err)
}
changed = true
}
if err := ioutil.WriteFile(path, item.Content, mode); err != nil {
return false, err
}
}
return changed, nil
}

// Loadf returns the file content read as byte slice
func Loadf(path string) []byte {
blob, err := ioutil.ReadFile(path)
if err != nil {
klog.Fatalf("Unable to load file %s: %v", path, err)
return nil
}
return blob
}
66 changes: 33 additions & 33 deletions hack/update/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ghCreatePR(ctx context.Context, owner, repo, base, branch, title string, is
}

// update files
changes, err := ghUpdate(ctx, owner, repo, baseTree, token, schema, data)
changes, err := ghUpdate(ctx, owner, repo, token, schema, data)
if err != nil {
return nil, fmt.Errorf("unable to update files: %w", err)
}
Expand Down Expand Up @@ -126,16 +126,16 @@ func ghCreatePR(ctx context.Context, owner, repo, base, branch, title string, is
klog.Infof("PR branch '%s' successfully created: %s", prBranch, prRef.GetURL())

// create PR
plan, err := GetPlan(schema, data)
_, pretty, err := GetPlan(schema, data)
if err != nil {
klog.Fatalf("Unable to parse schema: %v\n%s", err, plan)
klog.Fatalf("Unable to parse schema: %v\n%s", err, pretty)
}
modifiable := true
pr, _, err := ghc.PullRequests.Create(ctx, owner, repo, &github.NewPullRequest{
Title: github.String(title),
Head: github.String(*fork.Owner.Login + ":" + prBranch),
Base: github.String(base),
Body: github.String(fmt.Sprintf("fixes: #%d\n\nAutomatically created PR to update repo according to the Plan:\n\n```\n%s\n```", issue, plan)),
Body: github.String(fmt.Sprintf("fixes: #%d\n\nAutomatically created PR to update repo according to the Plan:\n\n```\n%s\n```", issue, pretty)),
MaintainerCanModify: &modifiable,
})
if err != nil {
Expand Down Expand Up @@ -170,40 +170,40 @@ func ghFindPR(ctx context.Context, title, owner, repo, base, token string) (url

// ghUpdate updates remote GitHub owner/repo tree according to the given token, schema and data.
// Returns resulting changes, and any error occurred.
func ghUpdate(ctx context.Context, owner, repo string, tree *github.Tree, token string, schema map[string]Item, data interface{}) (changes []*github.TreeEntry, err error) {
func ghUpdate(ctx context.Context, owner, repo string, token string, schema map[string]Item, data interface{}) (changes []*github.TreeEntry, err error) {
ghc := ghClient(ctx, token)

// load each schema item content and update it creating new GitHub TreeEntries
cnt := len(schema) // expected number of files to change
for _, org := range tree.Entries {
if *org.Type == "blob" {
if item, match := schema[*org.Path]; match {
blob, _, err := ghc.Git.GetBlobRaw(ctx, owner, repo, *org.SHA)
if err != nil {
return nil, fmt.Errorf("unable to get file: %w", err)
}
item.Content = blob
changed, err := item.apply(data)
if err != nil {
return nil, fmt.Errorf("unable to update file: %w", err)
}
if changed {
// add github.TreeEntry that will replace original path content with the updated one
changes = append(changes, &github.TreeEntry{
Path: org.Path,
Mode: org.Mode,
Type: org.Type,
Content: github.String(string(item.Content)),
})
}
if cnt--; cnt == 0 {
break
}
for path, item := range schema {
// if the item's content is already set, give it precedence over any current file content
var content string
if item.Content == nil {
file, _, _, err := ghc.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{Ref: ghBase})
if err != nil {
return nil, fmt.Errorf("unable to get file content: %w", err)
}
content, err = file.GetContent()
if err != nil {
return nil, fmt.Errorf("unable to read file content: %w", err)
}
item.Content = []byte(content)
}
if err := item.apply(data); err != nil {
return nil, fmt.Errorf("unable to update file: %w", err)
}
if content != string(item.Content) {
// add github.TreeEntry that will replace original path content with the updated one or add new if one doesn't exist already
// ref: https://developer.github.com/v3/git/trees/#tree-object
rcPath := path // make sure to copy path variable as its reference (not value!) is passed to changes
rcMode := "100644"
rcType := "blob"
changes = append(changes, &github.TreeEntry{
Path: &rcPath,
Mode: &rcMode,
Type: &rcType,
Content: github.String(string(item.Content)),
})
}
}
if cnt != 0 {
return nil, fmt.Errorf("unable to find all the files (%d missing) - check the Plan: %w", cnt, err)
}
return changes, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 1.1.1.1
bindPort: 12345
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
ttl: 24h0m0s
usages:
- signing
- authentication
nodeRegistration:
criSocket: /run/containerd/containerd.sock
name: "mk"
kubeletExtraArgs:
node-ip: 1.1.1.1
taints: []
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
apiServer:
certSANs: ["127.0.0.1", "localhost", "1.1.1.1"]
extraArgs:
enable-admission-plugins: "NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"
controllerManager:
extraArgs:
leader-elect: "false"
scheduler:
extraArgs:
leader-elect: "false"
certificatesDir: /var/lib/minikube/certs
clusterName: mk
controlPlaneEndpoint: control-plane.minikube.internal:12345
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/minikube/etcd
extraArgs:
proxy-refresh-interval: "70000"
kubernetesVersion: v1.19.0
networking:
dnsDomain: cluster.local
podSubnet: "10.244.0.0/16"
serviceSubnet: 10.96.0.0/12
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
authentication:
x509:
clientCAFile: /var/lib/minikube/certs/ca.crt
cgroupDriver: systemd
clusterDomain: "cluster.local"
# disable disk resource management by default
imageGCHighThresholdPercent: 100
evictionHard:
nodefs.available: "0%"
nodefs.inodesFree: "0%"
imagefs.available: "0%"
failSwapOn: false
staticPodPath: /etc/kubernetes/manifests
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
clusterCIDR: "10.244.0.0/16"
metricsBindAddress: 1.1.1.1:10249
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 1.1.1.1
bindPort: 8443
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
ttl: 24h0m0s
usages:
- signing
- authentication
nodeRegistration:
criSocket: /run/containerd/containerd.sock
name: "mk"
kubeletExtraArgs:
node-ip: 1.1.1.1
taints: []
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
apiServer:
certSANs: ["127.0.0.1", "localhost", "1.1.1.1"]
extraArgs:
enable-admission-plugins: "NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"
controllerManager:
extraArgs:
leader-elect: "false"
scheduler:
extraArgs:
leader-elect: "false"
certificatesDir: /var/lib/minikube/certs
clusterName: mk
controlPlaneEndpoint: control-plane.minikube.internal:8443
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/minikube/etcd
extraArgs:
proxy-refresh-interval: "70000"
kubernetesVersion: v1.19.0
networking:
dnsDomain: cluster.local
podSubnet: "192.168.32.0/20"
serviceSubnet: 10.96.0.0/12
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
authentication:
x509:
clientCAFile: /var/lib/minikube/certs/ca.crt
cgroupDriver: systemd
clusterDomain: "cluster.local"
# disable disk resource management by default
imageGCHighThresholdPercent: 100
evictionHard:
nodefs.available: "0%"
nodefs.inodesFree: "0%"
imagefs.available: "0%"
failSwapOn: false
staticPodPath: /etc/kubernetes/manifests
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
clusterCIDR: "192.168.32.0/20"
metricsBindAddress: 1.1.1.1:10249
67 changes: 67 additions & 0 deletions hack/update/kubernetes_version/templates/v1beta2/containerd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 1.1.1.1
bindPort: 8443
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
ttl: 24h0m0s
usages:
- signing
- authentication
nodeRegistration:
criSocket: /run/containerd/containerd.sock
name: "mk"
kubeletExtraArgs:
node-ip: 1.1.1.1
taints: []
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
apiServer:
certSANs: ["127.0.0.1", "localhost", "1.1.1.1"]
extraArgs:
enable-admission-plugins: "NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"
controllerManager:
extraArgs:
leader-elect: "false"
scheduler:
extraArgs:
leader-elect: "false"
certificatesDir: /var/lib/minikube/certs
clusterName: mk
controlPlaneEndpoint: control-plane.minikube.internal:8443
dns:
type: CoreDNS
etcd:
local:
dataDir: /var/lib/minikube/etcd
extraArgs:
proxy-refresh-interval: "70000"
kubernetesVersion: v1.19.0
networking:
dnsDomain: cluster.local
podSubnet: "10.244.0.0/16"
serviceSubnet: 10.96.0.0/12
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
authentication:
x509:
clientCAFile: /var/lib/minikube/certs/ca.crt
cgroupDriver: systemd
clusterDomain: "cluster.local"
# disable disk resource management by default
imageGCHighThresholdPercent: 100
evictionHard:
nodefs.available: "0%"
nodefs.inodesFree: "0%"
imagefs.available: "0%"
failSwapOn: false
staticPodPath: /etc/kubernetes/manifests
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
clusterCIDR: "10.244.0.0/16"
metricsBindAddress: 1.1.1.1:10249
Loading