Repository: vmware-tanzu/tanzu-framework (language: go)

Issue:
Add `core-management-plugins-package` as part of TKG meta package
**Bug description**

When creating management-cluster using `package-based-lcm` feature flag enabled, context-scoped CLIPlugin resources are not getting deployed on the management-cluster.

So, when using official building, `tanzu plugin sync` might not be able to find context-scoped plugins. 

**Affected product area (please put an X in all that apply)**

- [ ] APIs
- [ ] Addons
- [x] CLI
- [ ] Docs
- [ ] IAM
- [ ] Installation
- [x] Plugin
- [ ] Security
- [ ] Test and Release
- [ ] User Experience

**Expected behavior**
CLIPlugin should be available 


**Steps to reproduce the bug**

**Version** (include the SHA if the version is not obvious)

**Environment where the bug was observed (cloud, OS, etc)**

**Relevant Debug Output (Logs, manifests, etc)**



Discussion / hints:

Fix patch:
diff --git a/packages/core-management-plugins/bundle/config/cliplugin-crd.yaml b/packages/core-management-plugins/bundle/config/cliplugin-crd.yaml
index 4ad9a80b9a..f19c8f0059 100644
--- a/packages/core-management-plugins/bundle/config/cliplugin-crd.yaml
+++ b/packages/core-management-plugins/bundle/config/cliplugin-crd.yaml
@@ -1,3 +1,8 @@
+#@ load("@ytt:data", "data")
+#@ load("@ytt:yaml", "yaml")
+
+#@ if data.values.deployCLIPluginCRD:
+
 ---
 apiVersion: apiextensions.k8s.io/v1
 kind: CustomResourceDefinition
@@ -102,3 +107,5 @@ spec:
           type: object
       served: true
       storage: true
+
+#@ end
diff --git a/packages/core-management-plugins/bundle/config/values.yaml b/packages/core-management-plugins/bundle/config/values.yaml
new file mode 100644
index 0000000000..b313602935
--- /dev/null
+++ b/packages/core-management-plugins/bundle/config/values.yaml
@@ -0,0 +1,5 @@
+#@data/values
+#@overlay/match-child-defaults missing_ok=True
+
+---
+deployCLIPluginCRD: true
diff --git a/packages/tkg/bundle/config/packageinstalls/core-management-plugins-pi.yaml b/packages/tkg/bundle/config/packageinstalls/core-management-plugins-pi.yaml
new file mode 100644
index 0000000000..e71f35e375
--- /dev/null
+++ b/packages/tkg/bundle/config/packageinstalls/core-management-plugins-pi.yaml
@@ -0,0 +1,40 @@
+#@ load("@ytt:data", "data")
+#@ load("@ytt:yaml", "yaml")
+
+---
+apiVersion: packaging.carvel.dev/v1alpha1
+kind: PackageInstall
+metadata:
+  name: core-management-plugins
+  namespace: #@ data.values.coreManagementPluginsPackage.namespaceForPackageInstallation
+  annotations:
+    kapp.k14s.io/change-rule.0: "upsert after upserting framework-packageinstall/tanzu-framework"
+    kapp.k14s.io/change-rule.1: "upsert after upserting core-management-plugins-packageinstall/serviceaccount"
+    kapp.k14s.io/change-rule.2: "delete before deleting core-management-plugins-packageinstall/serviceaccount"
+
+  labels:
+    tkg.tanzu.vmware.com/package-type: "management"
+spec:
+  serviceAccountName: core-management-plugins-package-sa
+  packageRef:
+    refName: core-management-plugins.tanzu.vmware.com
+    versionSelection:
+      prereleases: {}
+      #@ if data.values.coreManagementPluginsPackage.versionConstraints:
+      constraints: #@ data.values.coreManagementPluginsPackage.versionConstraints
+      #@ end
+  values:
+    - secretRef:
+        name: core-management-plugins-values
+---
+apiVersion: v1
+kind: Secret
+metadata:
+  name: core-management-plugins-values
+  namespace: #@ data.values.coreManagementPluginsPackage.namespaceForPackageInstallation
+  annotations:
+    kapp.k14s.io/change-rule.0: "upsert after upserting core-management-plugins-packageinstall/serviceaccount"
+    kapp.k14s.io/change-rule.1: "delete before deleting core-management-plugins-packageinstall/serviceaccount"
+type: Opaque
+stringData:
+  values.yaml: #@ yaml.encode(data.values.coreManagementPluginsPackage.coreManagementPluginsPackageValues)
diff --git a/packages/tkg/bundle/config/packageinstalls/core-management-plugins-service-account.yaml b/packages/tkg/bundle/config/packageinstalls/core-management-plugins-service-account.yaml
new file mode 100644
index 0000000000..7b3581fe4e
--- /dev/null
+++ b/packages/tkg/bundle/config/packageinstalls/core-management-plugins-service-account.yaml
@@ -0,0 +1,39 @@
+#@ load("@ytt:data", "data")
+
+---
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+  name: core-management-plugins-package-sa
+  namespace: #@ data.values.coreManagementPluginsPackage.namespaceForPackageInstallation
+  annotations:
+    kapp.k14s.io/change-group: "core-management-plugins-packageinstall/serviceaccount-0"
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRole
+metadata:
+  name: core-management-plugins-package-role
+  annotations:
+    kapp.k14s.io/change-group: "core-management-plugins-packageinstall/serviceaccount-0"
+rules:
+  #  RBAC rules to create PackageInstall CR and service accounts
+  - apiGroups: ["*"]
+    resources: ["*"]
+    verbs: ["*"]
+---
+apiVersion: rbac.authorization.k8s.io/v1
+kind: ClusterRoleBinding
+metadata:
+  name: core-management-plugins-package-cluster-rolebinding
+  annotations:
+    kapp.k14s.io/change-group: "core-management-plugins-packageinstall/serviceaccount"
+    kapp.k14s.io/change-rule.0: "upsert after upserting core-management-plugins-packageinstall/serviceaccount-0"
+    kapp.k14s.io/change-rule.1: "delete before deleting core-management-plugins-packageinstall/serviceaccount-0"
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: ClusterRole
+  name: core-management-plugins-package-role
+subjects:
+  - kind: ServiceAccount
+    name: core-management-plugins-package-sa
+    namespace: #@ data.values.coreManagementPluginsPackage.namespaceForPackageInstallation
diff --git a/packages/tkg/bundle/config/values.yaml b/packages/tkg/bundle/config/values.yaml
index 368a0a1b20..430708b46a 100644
--- a/packages/tkg/bundle/config/values.yaml
+++ b/packages/tkg/bundle/config/values.yaml
@@ -81,3 +81,9 @@ tkrSourceControllerPackage:
       httpProxy:
       httpsProxy:
       noProxy:
+
+coreManagementPluginsPackage:
+  namespaceForPackageInstallation: tkg-system
+  versionConstraints:
+  coreManagementPluginsPackageValues:
+    deployCLIPluginCRD: false
diff --git a/pkg/v1/tkg/managementcomponents/helper.go b/pkg/v1/tkg/managementcomponents/helper.go
index fa5f976640..1b9691747b 100644
--- a/pkg/v1/tkg/managementcomponents/helper.go
+++ b/pkg/v1/tkg/managementcomponents/helper.go
@@ -78,6 +78,9 @@ func GetTKGPackageConfigValuesFileFromUserConfig(managementPackageVersion string
 				TKRRepoImagePath:     tkrRepoImagePath,
 			},
 		},
+		CoreManagementPluginsPackage: CoreManagementPluginsPackage{
+			VersionConstraints: managementPackageVersion,
+		},
 	}
 
 	configBytes, err := yaml.Marshal(tkgPackageConfig)
diff --git a/pkg/v1/tkg/managementcomponents/types.go b/pkg/v1/tkg/managementcomponents/types.go
index cf895180cb..1303b32acf 100644
--- a/pkg/v1/tkg/managementcomponents/types.go
+++ b/pkg/v1/tkg/managementcomponents/types.go
@@ -5,11 +5,12 @@ package managementcomponents
 
 // TKGPackageConfig defines TKG package configuration
 type TKGPackageConfig struct {
-	Metadata                   Metadata                   `yaml:"metadata"`
-	ConfigValues               map[string]string          `yaml:"configvalues"`
-	FrameworkPackage           FrameworkPackage           `yaml:"frameworkPackage"`
-	ClusterClassPackage        ClusterClassPackage        `yaml:"clusterclassPackage"`
-	TKRSourceControllerPackage TKRSourceControllerPackage `yaml:"tkrSourceControllerPackage"`
+	Metadata                     Metadata                     `yaml:"metadata"`
+	ConfigValues                 map[string]string            `yaml:"configvalues"`
+	FrameworkPackage             FrameworkPackage             `yaml:"frameworkPackage"`
+	ClusterClassPackage          ClusterClassPackage          `yaml:"clusterclassPackage"`
+	TKRSourceControllerPackage   TKRSourceControllerPackage   `yaml:"tkrSourceControllerPackage"`
+	CoreManagementPluginsPackage CoreManagementPluginsPackage `yaml:"coreManagementPluginsPackage"`
 }
 
 // Metadata specifies metadata as part of TKG package config
@@ -54,6 +55,15 @@ type AddonsFeatureGates struct {
 type TanzuAddonsManager struct {
 	FeatureGates AddonsFeatureGates `yaml:"featureGates,omitempty"`
 }
+type CoreManagementPluginsPackage struct {
+	NamespaceForPackageInstallation   string                            `yaml:"namespaceForPackageInstallation,omitempty"`
+	VersionConstraints                string                            `yaml:"versionConstraints,omitempty"`
+	CoreManagementPluginsPackageValue CoreManagementPluginsPackageValue `yaml:"clusterclassInfraPackageValues,omitempty"`
+}
+
+type CoreManagementPluginsPackageValue struct {
+	DeployCLIPluginCRD bool `yaml:"deployCLIPluginCRD,omitempty"`
+}
 
 type AddonsManagerPackageValues struct {
 	VersionConstraints string             `yaml:"versionConstraints,omitempty"`


Test patch: