Skip to content

Commit 0967ec8

Browse files
authored
Merge pull request #119 from layer5io/feature/annotation
changes for annotation injection
2 parents fd04e92 + 4e4d80a commit 0967ec8

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/sirupsen/logrus v1.7.0
1111
golang.org/x/net v0.0.0-20200707034311-ab3426394381
1212
google.golang.org/grpc v1.32.0
13+
k8s.io/api v0.17.4
1314
k8s.io/apiextensions-apiserver v0.17.4
1415
k8s.io/apimachinery v0.17.4
1516
k8s.io/client-go v0.17.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ k8s.io/apimachinery v0.17.4/go.mod h1:gxLnyZcGNdZTCLnq3fgzyg2A5BVCHTNDFrw8AmuJ+0
447447
k8s.io/apiserver v0.17.4/go.mod h1:5ZDQ6Xr5MNBxyi3iUZXS84QOhZl+W7Oq2us/29c0j9I=
448448
k8s.io/client-go v0.17.4 h1:VVdVbpTY70jiNHS1eiFkUt7ZIJX3txd29nDxxXH4en8=
449449
k8s.io/client-go v0.17.4/go.mod h1:ouF6o5pz3is8qU0/qYL2RnoxOPqgfuidYLowytyLJmc=
450+
k8s.io/client-go v1.5.1 h1:XaX/lo2/u3/pmFau8HN+sB5C/b4dc4Dmm2eXjBH4p1E=
451+
k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o=
450452
k8s.io/code-generator v0.17.4/go.mod h1:l8BLVwASXQZTo2xamW5mQNFCe1XPiAesVq7Y1t7PiQQ=
451453
k8s.io/component-base v0.17.4/go.mod h1:5BRqHMbbQPm2kKu35v3G+CpVq4K0RJKC7TRioF0I9lE=
452454
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=

linkerd/install.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"github.com/layer5io/meshery-linkerd/pkg/util"
3232
"github.com/pkg/errors"
3333
"github.com/sirupsen/logrus"
34+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35+
corev1 "k8s.io/api/core/v1"
3436
)
3537

3638
const (
@@ -64,10 +66,42 @@ type Release struct {
6466
Assets []*Asset `json:"assets,omitempty"`
6567
}
6668

69+
// AddAnnotation is used to mark namespaces for automatic sidecar injection (or not)
70+
func (iClient *Client) AddAnnotation(namespace string, remove bool) error{
71+
ns, err := iClient.k8sClientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{})
72+
if err != nil {
73+
_,err:=iClient.k8sClientset.CoreV1().Namespaces().Create(&corev1.Namespace{
74+
ObjectMeta: metav1.ObjectMeta{
75+
Name: namespace,
76+
}})
77+
if err!=nil {
78+
err = errors.Wrapf(err, "Unable to create namespace")
79+
return err
80+
}
81+
}
82+
83+
if ns.ObjectMeta.Annotations == nil {
84+
ns.ObjectMeta.Annotations = map[string]string{}
85+
}
86+
ns.ObjectMeta.Annotations["linkerd.io/inject"] = "enabled"
87+
88+
if remove {
89+
delete(ns.ObjectMeta.Annotations, "linkerd.io/inject");
90+
}
91+
92+
_, err = iClient.k8sClientset.CoreV1().Namespaces().Update(ns)
93+
if err != nil {
94+
err = errors.Wrapf(err, "Unable to update namespace with annotation")
95+
return err
96+
}
97+
return nil
98+
}
99+
100+
// getLatestRelease is to pull down the Linkerd packages
67101
func (iClient *Client) getLatestReleaseURL() error {
68102

69103
if iClient.linkerdReleaseDownloadURL == "" || time.Since(iClient.linkerdReleaseUpdatedAt) > cachePeriod {
70-
logrus.Debugf("API info url: %s", repoURL)
104+
logrus.Debugf("API info url: %s ", repoURL)
71105
resp, err := http.Get(repoURL)
72106
if err != nil {
73107
err = errors.Wrapf(err, "error getting latest version info")
@@ -118,6 +152,7 @@ func (iClient *Client) getLatestReleaseURL() error {
118152
return nil
119153
}
120154

155+
// downloadFile pulls the release packages
121156
func (iClient *Client) downloadFile(urlToDownload, localFile string) error {
122157
dFile, err := os.Create(localFile)
123158
if err != nil {
@@ -159,6 +194,7 @@ func (iClient *Client) downloadFile(urlToDownload, localFile string) error {
159194
return nil
160195
}
161196

197+
// downloadLinkerd pull down packages
162198
func (iClient *Client) downloadLinkerd() error {
163199
logrus.Debug("preparing to download the latest linkerd release")
164200
err := iClient.getLatestReleaseURL()
@@ -189,6 +225,7 @@ func (iClient *Client) downloadLinkerd() error {
189225
return nil
190226
}
191227

228+
// execute processes the command given to it
192229
func (iClient *Client) execute(command ...string) (string, string, error) {
193230
err := iClient.downloadLinkerd()
194231
if err != nil {
@@ -219,6 +256,7 @@ func (iClient *Client) execute(command ...string) (string, string, error) {
219256
return outb.String(), errb.String(), nil
220257
}
221258

259+
// getYAML retrieves remote yaml file
222260
func (iClient *Client) getYAML(remoteURL, localFile string) (string, error) {
223261

224262
proceedWithDownload := true

linkerd/linkerd.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ func (iClient *Client) ApplyOperation(ctx context.Context, arReq *meshes.ApplyRu
532532
return &meshes.ApplyRuleResponse{
533533
OperationId: arReq.OperationId,
534534
}, nil
535+
case injectLinkerd:
536+
err := iClient.AddAnnotation(arReq.Namespace, arReq.DeleteOp)
537+
if err != nil {
538+
return nil,err
539+
}
540+
return &meshes.ApplyRuleResponse{
541+
OperationId: arReq.OperationId,
542+
}, nil
535543
default:
536544
// tmpl, err := template.ParseFiles(path.Join("linkerd", "config_templates", op.templateName))
537545
// if err != nil {

linkerd/supported_ops.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
installBooksAppCommand = "install_booksapp"
3333
installHTTPBinApp = "install_http_bin"
3434
installIstioBookInfoApp = "install_istio_book_info"
35+
injectLinkerd="inject_linkerd"
3536
)
3637

3738
var supportedOps = map[string]supportedOperation{
@@ -61,4 +62,8 @@ var supportedOps = map[string]supportedOperation{
6162
templateName: "istiobookinfo.yaml",
6263
opType: meshes.OpCategory_SAMPLE_APPLICATION,
6364
},
65+
injectLinkerd: {
66+
name: "Inject Linkerd to monitor namespace",
67+
opType: meshes.OpCategory_CONFIGURE,
68+
},
6469
}

0 commit comments

Comments
 (0)