Skip to content

Commit ffcb433

Browse files
committed
[ckecli] add sabakan enable subcommand
"ckecli sabakan disable" no longer removes sabakan URL.
1 parent 0de8021 commit ffcb433

File tree

8 files changed

+107
-18
lines changed

8 files changed

+107
-18
lines changed

docs/ckecli.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,23 @@ Copy files between hosts via scp.
215215

216216
Control [sabakan integration feature](sabakan-integration.md).
217217

218+
### `ckecli sabakan enable|disable`
219+
220+
Enables/Disables sabakan integration.
221+
222+
The integration will run when:
223+
- It is *not* disabled, and
224+
- URL of sabakan is set with `ckecli sabakan set-url`, and
225+
- Cluster configuration template is set with `ckecli sabakan set-template`.
226+
218227
### `ckecli sabakan set-url URL`
219228

220-
Set URL of sabakan. This enables sabakan integration.
229+
Set URL of sabakan.
221230

222231
### `ckecli sabakan get-url`
223232

224233
Show stored URL of sabakan.
225234

226-
### `ckecli sabakan disable`
227-
228-
Disables sabakan integration and removes sabakan URL.
229-
230235
### `ckecli sabakan set-template FILE`
231236

232237
Set the cluster configuration template.

docs/schema.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Non-namespace resources omit `/<NAMESPACE>` part.
7676

7777
Configurations for [sabakan integration](sabakan-integration.md).
7878

79+
### `sabakan/disabled`
80+
81+
If this key exists and its value is `true`, sabakan integration is disabled.
82+
7983
### `sabakan/query-variables`
8084

8185
User-specified variables for the GraphQL query.

mtest/ckecli.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,11 @@ func TestCKECLI() {
103103
execSafeAt(host1, "rm", "-f", srcFile, dstFile)
104104
}
105105
})
106+
107+
It("should invoke sabakan subcommand successfully", func() {
108+
ckecliSafe("sabakan", "set-url", "http://localhost:10080")
109+
ckecliSafe("sabakan", "disable")
110+
ckecliSafe("sabakan", "enable")
111+
ckecliSafe("sabakan", "get-url")
112+
})
106113
}

pkg/ckecli/cmd/sabakan_disable.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package cmd
22

33
import (
4+
"context"
5+
46
"github.com/cybozu-go/well"
57
"github.com/spf13/cobra"
68
)
79

8-
// sabakanDisableCmd represents the "sabakan disable" command
910
var sabakanDisableCmd = &cobra.Command{
1011
Use: "disable",
1112
Short: "disable sabakan integration",
12-
Long: `Disable sabakan integration by removing stored sabakan URL.`,
13+
Long: `Disable sabakan integration.`,
1314

1415
RunE: func(cmd *cobra.Command, args []string) error {
15-
well.Go(storage.DeleteSabakanURL)
16+
well.Go(func(ctx context.Context) error {
17+
return storage.EnableSabakan(ctx, false)
18+
})
1619
well.Stop()
1720
return well.Wait()
1821
},

pkg/ckecli/cmd/sabakan_enable.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
6+
"github.com/cybozu-go/well"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var sabakanEnableCmd = &cobra.Command{
11+
Use: "enable",
12+
Short: "enable sabakan integration",
13+
Long: `Enable sabakan integration.`,
14+
15+
RunE: func(cmd *cobra.Command, args []string) error {
16+
well.Go(func(ctx context.Context) error {
17+
return storage.EnableSabakan(ctx, true)
18+
})
19+
well.Stop()
20+
return well.Wait()
21+
},
22+
}
23+
24+
func init() {
25+
sabakanCmd.AddCommand(sabakanEnableCmd)
26+
}

sabakan/integrate.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ func (ig integrator) StartWatch(ctx context.Context, ch chan<- struct{}) error {
5151
func (ig integrator) Do(ctx context.Context, leaderKey string) error {
5252
st := cke.Storage{Client: ig.etcd}
5353

54+
disabled, err := st.IsSabakanDisabled(ctx)
55+
if err != nil {
56+
return err
57+
}
58+
if disabled {
59+
return nil
60+
}
61+
5462
tmpl, rev, err := st.GetSabakanTemplate(ctx)
5563
switch err {
5664
case cke.ErrNotFound:

storage.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cke
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"errors"
@@ -31,6 +32,7 @@ const (
3132
KeyRecords = "records/"
3233
KeyRecordID = "records"
3334
KeyResourcePrefix = "resource/"
35+
KeySabakanDisabled = "sabakan/disabled"
3436
KeySabakanQueryVariables = "sabakan/query-variables"
3537
KeySabakanTemplate = "sabakan/template"
3638
KeySabakanURL = "sabakan/url"
@@ -573,6 +575,30 @@ func (s Storage) DeleteResource(ctx context.Context, key string) error {
573575
return err
574576
}
575577

578+
// IsSabakanDisabled returns true if sabakan integration is disabled.
579+
func (s Storage) IsSabakanDisabled(ctx context.Context) (bool, error) {
580+
resp, err := s.Get(ctx, KeySabakanDisabled)
581+
if err != nil {
582+
return false, err
583+
}
584+
if resp.Count == 0 {
585+
return false, nil
586+
}
587+
588+
if bytes.Equal([]byte("true"), resp.Kvs[0].Value) {
589+
return true, nil
590+
}
591+
return false, nil
592+
}
593+
594+
// EnableSabakan enables sabakan integration when flag is true.
595+
// When flag is false, sabakan integration is disabled.
596+
func (s Storage) EnableSabakan(ctx context.Context, flag bool) error {
597+
val := fmt.Sprint(!flag)
598+
_, err := s.Put(ctx, KeySabakanDisabled, val)
599+
return err
600+
}
601+
576602
// SetSabakanQueryVariables sets query variables for Sabakan.
577603
// Caller must validate the contents.
578604
func (s Storage) SetSabakanQueryVariables(ctx context.Context, vars string) error {
@@ -646,9 +672,3 @@ func (s Storage) GetSabakanURL(ctx context.Context) (string, error) {
646672
}
647673
return string(resp.Kvs[0].Value), nil
648674
}
649-
650-
// DeleteSabakanURL deletes URL of sabakan API.
651-
func (s Storage) DeleteSabakanURL(ctx context.Context) error {
652-
_, err := s.Delete(ctx, KeySabakanURL)
653-
return err
654-
}

storage_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,30 @@ func testStorageSabakan(t *testing.T) {
537537
t.Error(`u2 != u`, u2)
538538
}
539539

540-
err = s.DeleteSabakanURL(ctx)
540+
disabled, err := s.IsSabakanDisabled(ctx)
541541
if err != nil {
542542
t.Fatal(err)
543543
}
544+
if disabled {
545+
t.Error("sabakan integration should not be disabled by default")
546+
}
544547

545-
_, err = s.GetSabakanURL(ctx)
546-
if err != ErrNotFound {
547-
t.Error("URL was not removed")
548+
err = s.EnableSabakan(ctx, false)
549+
if err != nil {
550+
t.Fatal(err)
551+
}
552+
disabled, err = s.IsSabakanDisabled(ctx)
553+
if !disabled {
554+
t.Error("sabakan integration could not be disabled")
555+
}
556+
557+
err = s.EnableSabakan(ctx, true)
558+
if err != nil {
559+
t.Fatal(err)
560+
}
561+
disabled, err = s.IsSabakanDisabled(ctx)
562+
if disabled {
563+
t.Error("sabakan integration could not be re-enabled")
548564
}
549565
}
550566

0 commit comments

Comments
 (0)