@@ -5,13 +5,18 @@ package managementcomponents
5
5
6
6
import (
7
7
"context"
8
+ "encoding/json"
9
+ "fmt"
8
10
"os"
9
11
"time"
10
12
11
13
"github.com/pkg/errors"
12
14
"golang.org/x/sync/errgroup"
15
+ corev1 "k8s.io/api/core/v1"
16
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
13
17
"k8s.io/apimachinery/pkg/labels"
14
18
"k8s.io/apimachinery/pkg/selection"
19
+ "k8s.io/apimachinery/pkg/types"
15
20
crtclient "sigs.k8s.io/controller-runtime/pkg/client"
16
21
17
22
kappipkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
@@ -49,6 +54,76 @@ type ManagementComponentsInstallOptions struct {
49
54
ManagementPackageRepositoryOptions ManagementPackageRepositoryOptions
50
55
}
51
56
57
+ func pauseAddonSecretReconciliation (ctx context.Context , clusterClient clusterclient.Client , clusterName , addonSecreteName , namespace string ) error {
58
+ secret := & corev1.Secret {}
59
+ jsonPatch := []clusterclient.JSONPatch {
60
+ {
61
+ Op : "add" ,
62
+ Path : fmt .Sprintf ("/metadata/annotations/tkg.tanzu.vmware.com~1addon-paused" ),
63
+ Value : "" ,
64
+ },
65
+ }
66
+ payloadBytes , err := json .Marshal (jsonPatch )
67
+ if err != nil {
68
+ return errors .Wrap (err , "unable to generate json patch" )
69
+ }
70
+
71
+ err = clusterClient .PatchResource (secret , addonSecreteName , namespace , string (payloadBytes ), types .JSONPatchType , nil )
72
+ if apierrors .IsNotFound (err ) {
73
+ return nil
74
+ }
75
+ if err != nil {
76
+ return errors .Wrapf (err , "failed to pause %s secret reconciliation" , addonSecreteName )
77
+ }
78
+ return nil
79
+ }
80
+
81
+ func pausePackageInstallReconciliation (ctx context.Context , clusterClient clusterclient.Client , pkgiName , namespace string ) error {
82
+ pkgi := & kappipkg.PackageInstall {}
83
+ jsonPatch := []map [string ]interface {}{
84
+ {
85
+ "op" : "add" ,
86
+ "path" : "/spec/paused" ,
87
+ "value" : true ,
88
+ },
89
+ }
90
+ payloadBytes , err := json .Marshal (jsonPatch )
91
+ if err != nil {
92
+ return errors .Wrap (err , "unable to generate json patch" )
93
+ }
94
+ err = clusterClient .PatchResource (pkgi , pkgiName , namespace , string (payloadBytes ), types .JSONPatchType , nil )
95
+ if apierrors .IsNotFound (err ) {
96
+ return nil
97
+ }
98
+ if err != nil {
99
+ return errors .Wrapf (err , "failed to pause %s packageinstall reconciliation" , pkgiName )
100
+ }
101
+ return nil
102
+ }
103
+
104
+ // PausedAdoonLifecycleManagement pauses/unpauses the lifecycle management of addon package with given name and namespace
105
+ func PauseAddonLifecycleManagement (mcip * ManagementComponentsInstallOptions , addonName , namespace string ) error {
106
+ clusterClient , err := clusterclient .NewClient (mcip .ClusterOptions .Kubeconfig , mcip .ClusterOptions .Kubecontext , clusterclient.Options {})
107
+ clusterName , err := clusterClient .GetCurrentClusterName (mcip .ClusterOptions .Kubecontext )
108
+ if err != nil {
109
+ return err
110
+ }
111
+ pkgiName := fmt .Sprintf ("tanzu-%s" , addonName )
112
+ addonSecretName := fmt .Sprintf ("%s-%s-addon" , clusterName , pkgiName )
113
+
114
+ err = pauseAddonSecretReconciliation (context .TODO (), clusterClient , clusterName , addonSecretName , namespace )
115
+ if err != nil {
116
+ return err
117
+ }
118
+
119
+ err = pausePackageInstallReconciliation (context .TODO (), clusterClient , pkgiName , namespace )
120
+ if err != nil {
121
+ return err
122
+ }
123
+
124
+ return nil
125
+ }
126
+
52
127
// InstallManagementComponents installs the management component to cluster
53
128
func InstallManagementComponents (mcip * ManagementComponentsInstallOptions ) error {
54
129
clusterClient , err := clusterclient .NewClient (mcip .ClusterOptions .Kubeconfig , mcip .ClusterOptions .Kubecontext , clusterclient.Options {})
@@ -61,6 +136,16 @@ func InstallManagementComponents(mcip *ManagementComponentsInstallOptions) error
61
136
if err != nil {
62
137
return err
63
138
}
139
+
140
+ err = PauseAddonLifecycleManagement (mcip , "addons-manager" , constants .TkgNamespace )
141
+ if err != nil {
142
+ panic (err )
143
+ }
144
+
145
+ // TODO (adduarte) add pkgi delete call
146
+
147
+ // TODO (adduarte) add addonsecret delete call
148
+
64
149
if err = InstallManagementPackages (pkgClient , mcip .ManagementPackageRepositoryOptions ); err != nil {
65
150
// instead of throwing error here, wait for some additional time for packages to get reconciled successfully
66
151
// error will be thrown at the next step if packages are not reconciled after timeout value
0 commit comments