Skip to content

Commit ff12875

Browse files
authored
feat: add helm capabilities API versions (#186)
* feat: add helm capabilities API versions * fix poller
1 parent 731f5d9 commit ff12875

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

cmd/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func runAgent(opt *options, config *rest.Config, ctx context.Context, k8sClient
3434
os.Exit(1)
3535
}
3636

37-
sr, err := service.NewServiceReconciler(mgr.GetClient(), config, r, opt.restoreNamespace)
37+
sr, err := service.NewServiceReconciler(ctx, mgr.GetClient(), config, r, opt.restoreNamespace)
3838
if err != nil {
3939
setupLog.Error(err, "unable to create service reconciler")
4040
os.Exit(1)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/pluralsh/console-client-go v0.5.6
2323
github.com/pluralsh/controller-reconcile-helper v0.0.4
2424
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
25-
github.com/pluralsh/polly v0.1.8
25+
github.com/pluralsh/polly v0.1.9
2626
github.com/samber/lo v1.39.0
2727
github.com/spf13/pflag v1.0.5
2828
github.com/stretchr/testify v1.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ github.com/pluralsh/controller-reconcile-helper v0.0.4 h1:1o+7qYSyoeqKFjx+WgQTxD
532532
github.com/pluralsh/controller-reconcile-helper v0.0.4/go.mod h1:AfY0gtteD6veBjmB6jiRx/aR4yevEf6K0M13/pGan/s=
533533
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=
534534
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34/go.mod h1:IagWXKFYu6NTHzcJx2dJyrIlZ1Sv2PH3fhOtplA9qOs=
535-
github.com/pluralsh/polly v0.1.8 h1:fkF5fLNofN4CyOs89lQfKeZaSXgRe8MnXz9VK5MzvRU=
536-
github.com/pluralsh/polly v0.1.8/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
535+
github.com/pluralsh/polly v0.1.9 h1:x968ohGfOtX/YwNbZBPCvRPFhQglvYZ33KLf4Yt/5q0=
536+
github.com/pluralsh/polly v0.1.9/go.mod h1:W9IBX3e3xEjJuRjAQRfFJpH+UkNjddVY5YjMhyisQqQ=
537537
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
538538
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
539539
github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=

pkg/controller/service/reconciler.go

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ import (
88

99
console "github.com/pluralsh/console-client-go"
1010
clienterrors "github.com/pluralsh/deployment-operator/internal/errors"
11+
"github.com/pluralsh/deployment-operator/internal/utils"
12+
"github.com/pluralsh/deployment-operator/pkg/applier"
13+
"github.com/pluralsh/deployment-operator/pkg/client"
1114
"github.com/pluralsh/deployment-operator/pkg/controller"
15+
plrlerrors "github.com/pluralsh/deployment-operator/pkg/errors"
16+
"github.com/pluralsh/deployment-operator/pkg/manifests"
17+
manis "github.com/pluralsh/deployment-operator/pkg/manifests"
18+
"github.com/pluralsh/deployment-operator/pkg/manifests/template"
19+
"github.com/pluralsh/deployment-operator/pkg/ping"
20+
"github.com/pluralsh/deployment-operator/pkg/websocket"
1221
"github.com/pluralsh/polly/algorithms"
1322
"github.com/samber/lo"
1423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1524
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
"k8s.io/apimachinery/pkg/util/wait"
1627
"k8s.io/client-go/discovery"
1728
"k8s.io/client-go/kubernetes"
1829
"k8s.io/client-go/rest"
@@ -23,15 +34,6 @@ import (
2334
"sigs.k8s.io/cli-utils/pkg/inventory"
2435
"sigs.k8s.io/controller-runtime/pkg/log"
2536
"sigs.k8s.io/controller-runtime/pkg/reconcile"
26-
27-
"github.com/pluralsh/deployment-operator/internal/utils"
28-
"github.com/pluralsh/deployment-operator/pkg/applier"
29-
"github.com/pluralsh/deployment-operator/pkg/client"
30-
plrlerrors "github.com/pluralsh/deployment-operator/pkg/errors"
31-
"github.com/pluralsh/deployment-operator/pkg/manifests"
32-
manis "github.com/pluralsh/deployment-operator/pkg/manifests"
33-
"github.com/pluralsh/deployment-operator/pkg/ping"
34-
"github.com/pluralsh/deployment-operator/pkg/websocket"
3537
)
3638

3739
func init() {
@@ -67,7 +69,8 @@ type ServiceReconciler struct {
6769
pinger *ping.Pinger
6870
}
6971

70-
func NewServiceReconciler(consoleClient client.Client, config *rest.Config, refresh time.Duration, restoreNamespace string) (*ServiceReconciler, error) {
72+
func NewServiceReconciler(ctx context.Context, consoleClient client.Client, config *rest.Config, refresh time.Duration, restoreNamespace string) (*ServiceReconciler, error) {
73+
logger := log.FromContext(ctx)
7174
utils.DisableClientLimits(config)
7275

7376
_, deployToken := consoleClient.GetCredentials()
@@ -103,6 +106,19 @@ func NewServiceReconciler(consoleClient client.Client, config *rest.Config, refr
103106
if err != nil {
104107
return nil, err
105108
}
109+
if err := CapabilitiesAPIVersions(discoveryClient); err != nil {
110+
return nil, err
111+
}
112+
113+
go func() {
114+
//nolint:all
115+
_ = wait.PollImmediateInfinite(time.Minute*5, func() (done bool, err error) {
116+
if err := CapabilitiesAPIVersions(discoveryClient); err != nil {
117+
logger.Error(err, "can't fetch API versions")
118+
}
119+
return false, nil
120+
})
121+
}()
106122

107123
return &ServiceReconciler{
108124
ConsoleClient: consoleClient,
@@ -120,6 +136,29 @@ func NewServiceReconciler(consoleClient client.Client, config *rest.Config, refr
120136
}, nil
121137
}
122138

139+
func CapabilitiesAPIVersions(discoveryClient *discovery.DiscoveryClient) error {
140+
lists, err := discoveryClient.ServerPreferredResources()
141+
if err != nil {
142+
return err
143+
}
144+
for _, list := range lists {
145+
if len(list.APIResources) == 0 {
146+
continue
147+
}
148+
gv, err := schema.ParseGroupVersion(list.GroupVersion)
149+
if err != nil {
150+
continue
151+
}
152+
for _, resource := range list.APIResources {
153+
if len(resource.Verbs) == 0 {
154+
continue
155+
}
156+
template.APIVersions.Set(fmt.Sprintf("%s/%s", gv.String(), resource.Kind), true)
157+
}
158+
}
159+
return nil
160+
}
161+
123162
func (s *ServiceReconciler) GetPublisher() (string, websocket.Publisher) {
124163
return "service.event", &socketPublisher{
125164
svcQueue: s.SvcQueue,

pkg/manifests/template/helm.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
"time"
1414

1515
"github.com/gofrs/flock"
16+
cmap "github.com/orcaman/concurrent-map/v2"
1617
"github.com/pkg/errors"
1718
console "github.com/pluralsh/console-client-go"
1819
"github.com/pluralsh/polly/algorithms"
1920
"github.com/pluralsh/polly/fs"
21+
"github.com/samber/lo"
2022
"helm.sh/helm/v3/pkg/action"
2123
"helm.sh/helm/v3/pkg/chart"
2224
"helm.sh/helm/v3/pkg/chart/loader"
@@ -30,8 +32,6 @@ import (
3032
"k8s.io/client-go/util/homedir"
3133
"k8s.io/kubectl/pkg/cmd/util"
3234
"sigs.k8s.io/yaml"
33-
34-
"github.com/samber/lo"
3535
)
3636

3737
const (
@@ -55,11 +55,13 @@ func init() {
5555
settings.RepositoryCache = dir
5656
settings.RepositoryConfig = path.Join(dir, "repositories.yaml")
5757
settings.KubeInsecureSkipTLSVerify = true
58+
APIVersions = cmap.New[bool]()
5859
}
5960

6061
var settings = cli.New()
6162
var EnableHelmDependencyUpdate bool
6263
var DisableHelmTemplateDryRunServer bool
64+
var APIVersions cmap.ConcurrentMap[string, bool]
6365

6466
func debug(format string, v ...interface{}) {
6567
format = fmt.Sprintf("INFO: %s\n", format)
@@ -228,6 +230,7 @@ func (h *helm) templateHelm(conf *action.Configuration, release, namespace strin
228230
return nil, err
229231
}
230232
client.KubeVersion = vsn
233+
client.APIVersions = algorithms.MapKeys[string, bool](APIVersions.Items())
231234

232235
return client.Run(chart, values)
233236
}

0 commit comments

Comments
 (0)