Skip to content

Commit

Permalink
Merge pull request #150 from wuxs/feat/clean-invalid-enabled-tenants-…
Browse files Browse the repository at this point in the history
…in-plugin

feat: add fix subcommand to clean invalid enabled tenants in plugin
  • Loading branch information
wuxs authored Jan 9, 2023
2 parents 8c98c4b + 531a9de commit 9cad785
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 20 deletions.
65 changes: 65 additions & 0 deletions cmd/plugin/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package plugin

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/tkeel-io/cli/pkg/kubernetes"
"github.com/tkeel-io/cli/pkg/print"
)

var FixCmd = &cobra.Command{
Use: "fix",
Short: "Clean up invalid enabled tenants in the plugin",
Example: `
# Clean up all plugins
tkeel plugin fix
# Clean up the specified plugins
tkeel plugin fix [plugin-id ...]
`,
Run: func(cmd *cobra.Command, args []string) {
plugins := args
print.PendingStatusEvent(os.Stdout, "Clean invalid enabled tenants....")
daprStatus, err := kubernetes.CheckDapr()
if err != nil {
print.FailureStatusEvent(os.Stdout, err.Error())
os.Exit(1)
}
tenantList, err := kubernetes.TenantList()
if err != nil {
print.FailureStatusEvent(os.Stdout, err.Error())
os.Exit(1)
}
var tenants = make([]string, len(tenantList))
for _, tenant := range tenantList {
tenants = append(tenants, tenant.ID)
}

if len(plugins) == 0 {
pluginList, err := kubernetes.InstalledPlugin()
if err != nil {
print.FailureStatusEvent(os.Stdout, err.Error())
os.Exit(1)
}
plugins = make([]string, len(pluginList))
for i, plugin := range pluginList {
plugins[i] = plugin.Name
}
}

for _, plugin := range plugins {
err := kubernetes.CleanInvalidTenants(plugin, tenants, daprStatus.Namespace)
if err != nil {
print.FailureStatusEvent(os.Stdout, fmt.Sprintf("clean invalid tenants failed, plugin: %s, err: %s", plugin, err.Error()))
}
}
print.InfoStatusEvent(os.Stdout, "Invalid tenant cleanup completed")
},
}

func init() {
FixCmd.Flags().BoolP("help", "h", false, "Print this help message")
PluginCmd.AddCommand(FixCmd)
}
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module github.com/tkeel-io/cli

go 1.17
go 1.18

require (
github.com/AlecAivazis/survey/v2 v2.3.2
github.com/briandowns/spinner v1.6.1
github.com/dapr/cli v1.5.0
github.com/fatih/color v1.13.0
github.com/go-redis/redis/v8 v8.11.5
github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8
github.com/gorilla/websocket v1.4.2
github.com/gosuri/uitable v0.0.4
Expand All @@ -16,8 +17,8 @@ require (
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
github.com/tkeel-io/kit v0.0.0-20220522082406-248e4772e711
github.com/tkeel-io/tkeel v1.0.1-0.20220722033200-9ed37e162616
github.com/tkeel-io/tkeel-interface/openapi v0.0.0-20220624023618-32db91cf0860
github.com/tkeel-io/tkeel v0.4.2-0.20220525100311-b65416ac6109
github.com/tkeel-io/tkeel-interface/openapi v0.0.0-20220424073125-8edc0200490f
google.golang.org/protobuf v1.28.0
helm.sh/helm/v3 v3.7.2
k8s.io/api v0.23.4
Expand All @@ -32,7 +33,7 @@ require (
cloud.google.com/go v0.93.3 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.21 // indirect
github.com/Azure/go-autorest/autorest v0.11.23 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.16 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
Expand All @@ -53,8 +54,9 @@ require (
github.com/containerd/containerd v1.5.7 // indirect
github.com/containerd/continuity v0.1.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.2 // indirect
github.com/dapr/dapr v1.5.0-rc.3 // indirect
github.com/dapr/dapr v1.6.0-rc.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/docker/cli v20.10.7+incompatible // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.3+incompatible // indirect
Expand Down Expand Up @@ -126,8 +128,8 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rubenv/sql-migrate v0.0.0-20210614095031-55d5740dbbcc // indirect
github.com/russross/blackfriday v2.0.0+incompatible // indirect
Expand All @@ -150,7 +152,7 @@ require (
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
Expand Down
32 changes: 20 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ github.com/Azure/go-autorest/autorest v0.11.1/go.mod h1:JFgpikqFJ/MleTTxwepExTKn
github.com/Azure/go-autorest/autorest v0.11.3/go.mod h1:JFgpikqFJ/MleTTxwepExTKnFUKKszPS8UavbQYUMuw=
github.com/Azure/go-autorest/autorest v0.11.17/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
github.com/Azure/go-autorest/autorest v0.11.18/go.mod h1:dSiJPy22c3u0OtOKDNttNgqpNFY/GeWa7GH/Pz56QRA=
github.com/Azure/go-autorest/autorest v0.11.21 h1:w77zY/9RnUAWcIQyDC0Fc89mCvwftR8F+zsR/OH6enk=
github.com/Azure/go-autorest/autorest v0.11.21/go.mod h1:Do/yuMSW/13ayUkcVREpsMHGG+MvV81uzSCFgYPj4tM=
github.com/Azure/go-autorest/autorest v0.11.23 h1:bRQWsW25/YkoxnIqXMPF94JW33qWDcrPMZ3bINaAruU=
github.com/Azure/go-autorest/autorest v0.11.23/go.mod h1:BAWYUWGPEtKPzjVkp0Q6an0MJcJDsoh5Z1BFAEFs4Xs=
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
github.com/Azure/go-autorest/autorest/adal v0.8.0/go.mod h1:Z6vX6WXXuyieHAXwMj0S6HY6e6wcHn37qQMBQlvY3lc=
github.com/Azure/go-autorest/autorest/adal v0.8.1/go.mod h1:ZjhuQClTqx435SRJ2iMlOxPYt3d2C/T/7TiQCVZSn3Q=
Expand Down Expand Up @@ -456,8 +457,9 @@ github.com/danieljoos/wincred v1.1.0/go.mod h1:XYlo+eRTsVA9aHGp7NGjFkPla4m+DCL7h
github.com/dapr/cli v1.5.0 h1:i7lYoziT84NLvPnZf6IKWJ07whcTV51Q5D+cs+MG5Rs=
github.com/dapr/cli v1.5.0/go.mod h1:SZlShkjy4TopUWc/O9KjeSl5KzbxvnjiMfMucsxIntw=
github.com/dapr/components-contrib v1.5.0-rc.1/go.mod h1:ixc7KCpP0Ra91hQG+/e2nwSmA9ESlAdLrqjK/zvjyXE=
github.com/dapr/dapr v1.5.0-rc.3 h1:dgIyhIbKRKQ6afstbV4ah99ueNrcyVHX+31VK8dUcY4=
github.com/dapr/dapr v1.5.0-rc.3/go.mod h1:TLv3aJtm3yLNh8TDMHRz/Rkn8Oyx9l9O80RmXJZ1ZU0=
github.com/dapr/dapr v1.6.0-rc.2 h1:gywEsMOy4yNNAxq1kxX6LspAio3a9iOLS/1Lji0USPg=
github.com/dapr/dapr v1.6.0-rc.2/go.mod h1:1H5Nb+rPY7ukYL9kTk8VoAzpdkhqTHRG/Gxzt/XNFsY=
github.com/dapr/go-sdk v1.0.0/go.mod h1:zyhsocIKv4pqQ2VtvWvf2CK1UhP7Z2OAOXgEpVxMgIs=
github.com/dapr/kit v0.0.2-0.20210614175626-b9074b64d233/go.mod h1:y8r0VqUNKyd6xBXp7gQjwA59wlCLGfKzL5J8iJsN09w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -477,6 +479,7 @@ github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/didip/tollbooth v4.0.2+incompatible/go.mod h1:A9b0665CE6l1KmzpDws2++elm/CsuWBMa5Jv4WY0PEY=
Expand Down Expand Up @@ -672,6 +675,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis/v8 v8.8.0/go.mod h1:F7resOH5Kdug49Otu24RjHWwgK7u9AmtqWMnCV1iP5Y=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down Expand Up @@ -1300,8 +1305,8 @@ github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.15.0 h1:1V1NfVQR87RtWAgp1lv9JZJ5Jap+XFGKPi00andXGi4=
github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand All @@ -1311,8 +1316,8 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc=
github.com/onsi/gomega v1.10.5 h1:7n6FEkpFmfCoo2t+YYqXH0evK+a9ICQz0xcAy9dYcaQ=
github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/open-policy-agent/opa v0.23.2/go.mod h1:rrwxoT/b011T0cyj+gg2VvxqTtn6N3gp/jzmr3fjW44=
github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
Expand Down Expand Up @@ -1421,8 +1426,9 @@ github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt2
github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4=
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
github.com/prometheus/common v0.15.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s=
github.com/prometheus/common v0.26.0 h1:iMAkS2TDoNWnKM+Kopnx/8tnEStIfpYA0ur0xQzzhMQ=
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4=
github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls=
github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
Expand All @@ -1437,8 +1443,9 @@ github.com/prometheus/procfs v0.0.6/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+Gx
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/statsd_exporter v0.15.0/go.mod h1:Dv8HnkoLQkeEjkIE4/2ndAA7WL1zHKK7WMqFQqu72rw=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
Expand Down Expand Up @@ -1575,10 +1582,10 @@ github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhV
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tkeel-io/kit v0.0.0-20220522082406-248e4772e711 h1:sjomik8MznWAiB938kJ5l71w6MUwu9D17v0gt2KOUsY=
github.com/tkeel-io/kit v0.0.0-20220522082406-248e4772e711/go.mod h1:hTI3tXXqyXfc+eE1wjv1D1dEO4TgArDbaaKKl7RTCZI=
github.com/tkeel-io/tkeel v1.0.1-0.20220722033200-9ed37e162616 h1:il4LoNnMZlP9Gr1BFuXsU/Hjy25iCAbnOjMp0xgshrU=
github.com/tkeel-io/tkeel v1.0.1-0.20220722033200-9ed37e162616/go.mod h1:y2U9U15cXVZiHIdoksHF9oLC+iKjgC4pX7dLq2YemOQ=
github.com/tkeel-io/tkeel-interface/openapi v0.0.0-20220624023618-32db91cf0860 h1:k8EFmOucfdYpdz6EuD9q+wOOdkGMNzx9rApTPrksiBY=
github.com/tkeel-io/tkeel-interface/openapi v0.0.0-20220624023618-32db91cf0860/go.mod h1:tjJgHPar2pFjydhnOCLspww8tSOvcKch241PeP9OL8E=
github.com/tkeel-io/tkeel v0.4.2-0.20220525100311-b65416ac6109 h1:BbId2Aml2F3nQ9Y0cpFuXI2wFxsvkVnvrLWDhMlIgbI=
github.com/tkeel-io/tkeel v0.4.2-0.20220525100311-b65416ac6109/go.mod h1:VceDT1GuqVKu8Lq3nGv7GyuVIj+oQ89QGeiNntzJy9k=
github.com/tkeel-io/tkeel-interface/openapi v0.0.0-20220424073125-8edc0200490f h1:ceiytk2mifoDUxdKqCmaapngRv5EXNRQ2GQ+S25qI3Y=
github.com/tkeel-io/tkeel-interface/openapi v0.0.0-20220424073125-8edc0200490f/go.mod h1:tjJgHPar2pFjydhnOCLspww8tSOvcKch241PeP9OL8E=
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down Expand Up @@ -1874,6 +1881,7 @@ golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211209124913-491a49abca63 h1:iocB37TsdFuN6IBRZ+ry36wrkoV51/tl5vOWqkcPGvY=
golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down Expand Up @@ -2031,8 +2039,8 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/redis/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package redis

import (
"fmt"

"github.com/go-redis/redis/v8"
)

func NewClient(host string, port int, password string, db int) *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
Password: password, // no password set
DB: db, // use default DB
})
return rdb
}
90 changes: 90 additions & 0 deletions pkg/kubernetes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
package kubernetes

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/dapr/cli/pkg/kubernetes"
"github.com/tkeel-io/cli/pkg/client/redis"
terrors "github.com/tkeel-io/kit/errors"
tenantApi "github.com/tkeel-io/tkeel/api/tenant/v1"

Expand All @@ -19,6 +22,7 @@ import (
v1 "github.com/tkeel-io/tkeel-interface/openapi/v1"
pluginAPI "github.com/tkeel-io/tkeel/api/plugin/v1"
"google.golang.org/protobuf/encoding/protojson"
mate_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -438,3 +442,89 @@ func UninstallPlugin(pluginID string) error {
}
return nil
}

type PluginEnableInfo struct {
ID string `json:"id"`
Installer struct {
Repo string `json:"repo"`
Name string `json:"name"`
Version string `json:"version"`
Desc string `json:"desc"`
Maintainer []struct {
Name string `json:"name"`
URL string `json:"url"`
Email string `json:"email"`
} `json:"maintainer"`
} `json:"installer"`
TkeelVersion string `json:"tkeel_version"`
Secret string `json:"secret"`
RegisterTimestamp int `json:"register_timestamp"`
Version string `json:"version"`
Status int `json:"status"`
EnableTenantes []EnableTenant `json:"enable_tenantes"`
}
type EnableTenant struct {
TenantID string `json:"tenant_id"`
OperatorID string `json:"operator_id"`
EnableTimestamp int `json:"enable_timestamp"`
}

func CleanInvalidTenants(pluginID string, tenants []string, namespace string) error {
tenantMap := make(map[string]struct{})
for _, tenant := range tenants {
tenantMap[tenant] = struct{}{}
}
password, err := GetRedisPassword(namespace)
if err != nil {
return err
}
pf, err := GetPodPortForward("tkeel-middleware-redis-master-0", namespace, 6379)
if err != nil {
return err
}
defer pf.Stop()

err = pf.Init()
if err != nil {
return err
}

ctx := context.Background()
rdb := redis.NewClient("127.0.0.1", pf.LocalPort, password, 0)
res := rdb.HGet(ctx, fmt.Sprintf("rudder||p_%s", pluginID), "data")
if res.Err() != nil {
return res.Err()
}
data, err := res.Bytes()
info := &PluginEnableInfo{}
err = json.Unmarshal(data, info)
newEnableTenants := make([]EnableTenant, 0)
for _, tenant := range info.EnableTenantes {
if tenant.TenantID == "_tKeel_system" {
newEnableTenants = append(newEnableTenants, tenant)
} else if _, ok := tenantMap[tenant.TenantID]; ok {
newEnableTenants = append(newEnableTenants, tenant)
}
}
info.EnableTenantes = newEnableTenants
data, err = json.Marshal(info)
rdb.HSet(ctx, fmt.Sprintf("rudder||p_%s", pluginID), "data", string(data))
return nil
}

func GetRedisPassword(namespace string) (string, error) {
_, client, err := kubernetes.GetKubeConfigClient()
if err != nil {
return "", fmt.Errorf("get kube config error: %w", err)
}
opts := mate_v1.GetOptions{}
secret, err := client.CoreV1().Secrets(namespace).Get(context.TODO(), "tkeel-middleware-redis", opts)
if err != nil {
return "", fmt.Errorf("get secret error: %w", err)
}

if value := secret.Data["redis-password"]; value != nil {
return string(value), err
}
return "", fmt.Errorf("get redis password error")
}
Loading

0 comments on commit 9cad785

Please sign in to comment.