Skip to content

Commit

Permalink
finish first version of flux precheck
Browse files Browse the repository at this point in the history
  • Loading branch information
JaneLiuL committed May 31, 2021
1 parent 00bf8d8 commit 1f4d1bf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
6 changes: 6 additions & 0 deletions REAME.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
This git repo is for to get what will be change before the code merge to master and deploy to cluster.

How to build?
checkout this gitrepository and run `go build ./`

How to use?
./flux-precheck --kustomizationName=kustomizationname --kustomizationNamespace=namespace

30 changes: 12 additions & 18 deletions compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import (
"context"
"fmt"
"time"

//"context"
//"fmt"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/krusty"
kustypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/api/konfig"
"strings"
//"time"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"

"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -64,26 +59,24 @@ func parseApplyOutput(in []byte) map[string]string {



func CheckDeployByKustomization(client dynamic.Interface, kustomization kustomizev1.Kustomization) error{

func CheckDeployByKustomization(client dynamic.Interface, kustomization kustomizev1.Kustomization) ([]string, error){
var lastTimekustomizationDeployOutput []string
labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{
fmt.Sprintf("%s/name", kustomizev1.GroupVersion.Group): kustomization.GetName(),
fmt.Sprintf("%s/namespace", kustomizev1.GroupVersion.Group): kustomization.GetNamespace(),
}}
glog.Infof("label is %s", labels.Set(labelSelector.MatchLabels).String())


// read status snapshot
if !kustomization.Spec.Prune || kustomization.Status.Snapshot == nil {
return nil
return lastTimekustomizationDeployOutput, nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second*120))
defer cancel()

for ns, gvks := range kustomization.Status.Snapshot.NamespacedKinds() {
for _, gvk := range gvks {
glog.Infof("namespace is %s, group is %s, version is %s, resource is %s", ns, gvk.Group, gvk.Version, gvk.Kind)
glog.Infof("label string is %s", labels.Set(labelSelector.MatchLabels).String())

gvr := schema.GroupVersionResource{
Group: gvk.Group,
Expand All @@ -96,8 +89,9 @@ func CheckDeployByKustomization(client dynamic.Interface, kustomization kustomiz
})
if err == nil {
for _, item := range resourceList.Items {
id := fmt.Sprintf("%s/%s/%s", item.GetKind(), item.GetNamespace(), item.GetName())
glog.Infof("resource is %s", id)
id := fmt.Sprintf("%s/%s/%s", fmt.Sprint(strings.ToLower(gvk.Kind),"s"), item.GetNamespace(), item.GetName())

lastTimekustomizationDeployOutput = append(lastTimekustomizationDeployOutput, id)
}
} else {
glog.Infof("client query failed for %s: %v", gvk.Kind, err)
Expand All @@ -119,16 +113,16 @@ func CheckDeployByKustomization(client dynamic.Interface, kustomization kustomiz
})
if err == nil {
for _, item := range resourceList.Items {
id := fmt.Sprintf("%s/%s", item.GetKind(), item.GetName())
glog.Infof("resource is %s", id)
//id := fmt.Sprintf("%s/%s", item.GetKind(), item.GetName())
id := fmt.Sprintf("%s/%s", fmt.Sprint(strings.ToLower(gvk.Kind),"s"), item.GetName())
//glog.Infof("resource is %s", id)
lastTimekustomizationDeployOutput = append(lastTimekustomizationDeployOutput, id)
}
} else {
glog.Infof("client query failed for %s: %v", gvk.Kind, err)
}


}
return nil
// get the resource with label, if the resource name not in the channel, then output as delete

return lastTimekustomizationDeployOutput, nil
}
46 changes: 29 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ import (
"k8s.io/client-go/dynamic"
)

type ResourceOutput struct {
action string
kind string
name string
}

const outputManifestFile = "flux-precheck-output-manifest.yaml"

Expand All @@ -36,10 +31,10 @@ func main() {
clientset *kubernetes.Clientset
data []byte
)
flag.StringVar(&manifestFolder, "manifest folder", "./", "The manifest folder")
flag.StringVar(&kustomizationName, "kustomization object name", "kustomization-name", "The kustomization object name")
flag.StringVar(&kustomizationNamespace, "kustomization object namespace", "default", "The kustomization object namespace")
flag.StringVar(&kubeconfigPath, "Kube config file path", "/root/.kube/config", "The kube config file path")
flag.StringVar(&manifestFolder, "manifestFolder", "./", "The manifest folder")
flag.StringVar(&kustomizationName, "kustomizationName", "kustomization-name", "The kustomization object name")
flag.StringVar(&kustomizationNamespace, "kustomizationNamespace", "kustomization-namespace", "The kustomization object namespace")
flag.StringVar(&kubeconfigPath, "kubeconfigPath", "/root/.kube/config", "The kube config file path")
flag.Parse()

// compile and dry run apply and output the create and configured
Expand All @@ -65,7 +60,7 @@ func main() {
defer cancel()

cmd := fmt.Sprintf("kubectl apply -f %s --dry-run=client", manifestsFile)
glog.Infof("command output as %s",cmd)

command := exec.CommandContext(applyCtx, "/bin/sh", "-c", cmd)

output, err := command.CombinedOutput()
Expand All @@ -76,19 +71,16 @@ func main() {
outputresources := parseApplyOutput(output)
glog.Infof(fmt.Sprintf("dry run output %s", outputresources))
//output format: map[Warning::kubectl deployment.apps/my-dep:configured pod/test:created]
var resourceoutput []ResourceOutput

resourceoutput := make(map[string]string)
for obj, action := range outputresources {

if obj == "Warning:" {
glog.Info("Skip warning")
continue
}
resourceoutput = append(resourceoutput, ResourceOutput{
action: action,
kind: strings.Split(obj, "/")[0],
name: strings.Split(obj, "/")[1],
})
resourceoutput[fmt.Sprintf("%s/%s", strings.Split(obj, "/")[0],strings.Split(obj, "/")[1])] = action

}

// read kustomization object name and namespace, read the status
Expand Down Expand Up @@ -121,8 +113,28 @@ func main() {
}

client, err := dynamic.NewForConfig(config)
_ = CheckDeployByKustomization(client, kustomization)
lastTimekustomizationDeployOutput, err := CheckDeployByKustomization(client, kustomization)


// As we already know what will be created / unchanged / configured by dry-run
// we need to compare with CheckDeployByKustomization output, to know what will be deleted


for _, v := range lastTimekustomizationDeployOutput {
ss := strings.Split(v, "/")


if _, found := resourceoutput[fmt.Sprintf("%s/%s", ss[0], ss[len(ss)-1])] ; found {
fmt.Println(v)
} else {
// this resource will be delete
resourceoutput[fmt.Sprintf("%s/%s", ss[0],ss[len(ss)-1])] = "deleted"
}
}

glog.Info("============result==============")

for mapk, mapv := range resourceoutput {
glog.Infof("%s action is %s", mapk, mapv)
}
}

0 comments on commit 1f4d1bf

Please sign in to comment.