forked from fluxcd/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
104 lines (91 loc) · 2.67 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
This package is for maintaining the link between `FluxHelmRelease`
resources and the Helm releases to which they
correspond. Specifically,
1. updating the `FluxHelmRelease` status based on the state of the
associated Helm release; and,
2. attributing each resource in a Helm release (under our control) to
the associated `FluxHelmRelease`.
*/
package status
import (
"encoding/json"
"time"
"github.com/go-kit/kit/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
kube "k8s.io/client-go/kubernetes"
"k8s.io/helm/pkg/helm"
fluxhelmtypes "github.com/weaveworks/flux/apis/helm.integrations.flux.weave.works/v1alpha2"
fluxhelm "github.com/weaveworks/flux/integrations/client/clientset/versioned"
"github.com/weaveworks/flux/integrations/helm/release"
)
const period = 10 * time.Second
type Updater struct {
fluxhelm fluxhelm.Interface
kube kube.Interface
helmClient *helm.Client
}
func New(fhrClient fluxhelm.Interface, kubeClient kube.Interface, helmClient *helm.Client) *Updater {
return &Updater{
fluxhelm: fhrClient,
kube: kubeClient,
helmClient: helmClient,
}
}
func (a *Updater) Loop(stop <-chan struct{}, logger log.Logger) {
ticker := time.NewTicker(period)
var logErr error
bail:
for {
select {
case <-stop:
break bail
case <-ticker.C:
}
// Look up FluxHelmReleases
namespaces, err := a.kube.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
logErr = err
break bail
}
for _, ns := range namespaces.Items {
fhrIf := a.fluxhelm.HelmV1alpha2().FluxHelmReleases(ns.Name)
fhrs, err := fhrIf.List(metav1.ListOptions{})
if err != nil {
logErr = err
break bail
}
for _, fhr := range fhrs.Items {
releaseName := release.GetReleaseName(fhr)
content, err := a.helmClient.ReleaseContent(releaseName)
if err != nil {
logger.Log("err", err)
continue
}
status := content.GetRelease().GetInfo().GetStatus()
if status.GetCode().String() != fhr.Status.ReleaseStatus {
newStatus := fluxhelmtypes.FluxHelmReleaseStatus{
ReleaseStatus: status.GetCode().String(),
}
var patchBytes []byte
if patchBytes, err = json.Marshal(map[string]interface{}{
"status": newStatus,
}); err == nil {
// CustomResources don't get
// StrategicMergePatch, for now, but since we
// want to unconditionally set the value, this
// is OK.
_, err = fhrIf.Patch(fhr.Name, types.MergePatchType, patchBytes)
}
if err != nil {
logger.Log("namespace", ns.Name, "resource", fhr.Name, "err", err)
continue
}
}
}
}
}
ticker.Stop()
logger.Log("loop", "stopping", "err", logErr)
}