Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into package-plugin-add-reconcilliation-wait
Browse files Browse the repository at this point in the history
Signed-off-by: Marjan Alavi <malavi@vmware.com>
  • Loading branch information
maralavi committed Aug 19, 2021
2 parents 36ec270 + fb15151 commit 69c9425
Show file tree
Hide file tree
Showing 104 changed files with 3,102 additions and 1,070 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-framework/pkg/v1/cli.BuildVersion=
LD_FLAGS += -X 'main.BuildEdition=$(BUILD_EDITION)'
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/buildinfo.IsOfficialBuild=$(IS_OFFICIAL_BUILD)'

ifneq ($(strip $(TANZU_CORE_BUCKET)),)
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-framework/pkg/v1/config.CoreBucketName=$(TANZU_CORE_BUCKET)'
endif

ifeq ($(TANZU_FORCE_NO_INIT), true)
LD_FLAGS += -X 'github.com/vmware-tanzu/tanzu-framework/pkg/v1/cli/command/core.forceNoInit=true'
endif

BUILD_TAGS ?=

ARTIFACTS_DIR ?= ./artifacts
Expand Down
2 changes: 1 addition & 1 deletion addons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ imageInfo:
tag: <image-tag-2>
...
```
The `<image-name-x>` should be exactly the same as the image names for your addon in [BOM](https://gitlab.eng.vmware.com/TKG/bolt/bolt-release-yamls/-/blob/5960e2c98ea83610624982eaec970c9b52cdc9c5/component/tkr-bom/tkr-bom-v1.20.4+vmware.1-tkg.1.yaml#L70-75)
The `<image-name-x>` should be exactly the same as the image names for your addon in TKR BOM.


### Makefile
Expand Down
2 changes: 2 additions & 0 deletions addons/controllers/addon_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -344,6 +345,7 @@ var _ = Describe("Addon Reconciler", func() {

Expect(ipkg.Spec.ServiceAccountName).Should(Equal(addonServiceAccount))

Expect(ipkg.Spec.SyncPeriod).Should(Equal(&metav1.Duration{Duration: appSyncPeriod}))
Expect(ipkg.Spec.PackageRef).ShouldNot(BeNil())
Expect(ipkg.Spec.PackageRef.RefName).Should(Equal("antrea.vmware.com"))
Expect(ipkg.Spec.PackageRef.VersionSelection.Prereleases).ShouldNot(Equal(nil))
Expand Down
19 changes: 10 additions & 9 deletions addons/controllers/package_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,17 @@ func (r *PackageReconciler) ReconcileAddonKappResourceNormal( // nolint:funlen
ipkg.ObjectMeta.Annotations[addontypes.AddonNamespaceAnnotation] = addonSecret.Namespace
ipkg.ObjectMeta.Annotations[addontypes.YttMarkerAnnotation] = util.GenerateAppSecretNameFromAddonSecret(addonSecret)

ipkg.Spec = pkgiv1alpha1.PackageInstallSpec{
ServiceAccountName: r.Config.AddonServiceAccount,
PackageRef: &pkgiv1alpha1.PackageRef{
RefName: addonConfig.PackageName,
VersionSelection: &versions.VersionSelectionSemver{
Prereleases: &versions.VersionSelectionSemverPrereleases{},
},
ipkg.Spec.SyncPeriod = &metav1.Duration{Duration: r.Config.AppSyncPeriod}
ipkg.Spec.ServiceAccountName = r.Config.AddonServiceAccount
ipkg.Spec.PackageRef = &pkgiv1alpha1.PackageRef{
RefName: addonConfig.PackageName,
VersionSelection: &versions.VersionSelectionSemver{
Prereleases: &versions.VersionSelectionSemverPrereleases{},
},
Values: []pkgiv1alpha1.PackageInstallValues{
{SecretRef: &pkgiv1alpha1.PackageInstallValuesSecretRef{Name: util.GenerateAppSecretNameFromAddonSecret(addonSecret)}},
}
ipkg.Spec.Values = []pkgiv1alpha1.PackageInstallValues{
{SecretRef: &pkgiv1alpha1.PackageInstallValuesSecretRef{
Name: util.GenerateAppSecretNameFromAddonSecret(addonSecret)},
},
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/cli/plugin/managementcluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type initRegionOptions struct {
deployTKGonVsphere7 bool
unattended bool
dryRun bool
forceConfigUpdate bool
clusterConfigFile string
plan string
clusterName string
Expand Down Expand Up @@ -128,6 +129,8 @@ func init() {
createCmd.Flags().StringToStringVarP(&iro.featureFlags, "feature-flags", "", nil, "Activate and deactivate hidden features in the form 'feature1=true,feature2=false'")
createCmd.Flags().MarkHidden("feature-flags") //nolint

createCmd.Flags().BoolVar(&iro.forceConfigUpdate, "force-config-update", false, "Force an update of all configuration files in ${HOME}/.config/tanzu/tkg/bom and ${HOME}/.tanzu/tkg/compatibility")

createCmd.Flags().SetNormalizeFunc(aliasNormalizeFunc)
}

Expand All @@ -139,7 +142,7 @@ func aliasNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
}

func runInit() error {
forceUpdateTKGCompatibilityImage := true
forceUpdateTKGCompatibilityImage := iro.forceConfigUpdate
tkgClient, err := newTKGCtlClient(forceUpdateTKGCompatibilityImage)
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions cmd/cli/plugin/managementcluster/post_install_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package main

func postInstallHook() error {
// To initialize configuration we do not need to do anything
// except creating tkgctl client. As initializing the client
// initializes the configuration to the tanzu config directory.
// Creating a tkgctl client in turn initializes the TKG configuration in the tanzu config directory.
forceUpdateTKGCompatibilityImage := true
_, err := newTKGCtlClient(forceUpdateTKGCompatibilityImage)
if err != nil {
Expand Down
14 changes: 4 additions & 10 deletions cmd/cli/plugin/package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ Use "tanzu package repository [command] --help" for more information about a com
An example values.yaml is as follows:
```sh
---
fluent_bit:
outputs: |
outputs: |
[OUTPUT]
Name stdout
Match *
Expand Down Expand Up @@ -198,9 +196,7 @@ Use "tanzu package repository [command] --help" for more information about a com
/ Retrieving installation details for myfb...
cat config.yaml
---
fluent_bit:
outputs: |
outputs: |
[OUTPUT]
Name stdout
Match *
Expand Down Expand Up @@ -245,12 +241,10 @@ Use "tanzu package repository [command] --help" for more information about a com
An example values.yaml is as follows:
```sh
---
fluent_bit:
outputs: |
outputs: |
[OUTPUT]
Name stdout
Match *
Match /
```
11. Uninstall a package
Expand Down
2 changes: 2 additions & 0 deletions docs/api-machinery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Documentation for API Machinery related things.
## Table of Contents

* [Tanzu Framework Controllers Using Kubebuilder](tanzu-core-controllers.md)
* [Features and FeatureGates](features-and-featuregates.md)
* [Capability Discovery](capability-discovery.md)
197 changes: 197 additions & 0 deletions docs/api-machinery/capability-discovery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
Table of Contents
=================

* [Capability Discovery](#capability-discovery)
* [Discovery Go Package](#discovery-go-package)
* [Building a ClusterQueryClient](#building-a-clusterqueryclient)
* [Building and Executing Queries](#building-and-executing-queries)
* [Executing Pre-defined TKG queries](#executing-pre-defined-tkg-queries)
* [Capability CRD](#capability-crd)
* [Example Capability Custom Resource](#example-capability-custom-resource)

# Capability Discovery

The capability discovery Go package `pkg/v1/sdk/capabilities/discovery`, along with the `Capability` CRD offer the
ability to query a cluster's capabilities. A "capability" is defined as anything a Kubernetes cluster can do or have,
such as objects and the API surface area. Capability discovery can be used to answer questions such
as `Is this a TKG cluster?`, `Does this cluster have a resource Foo?` etc.

## Discovery Go Package

The [`pkg/v1/sdk/capabilities/discovery`](https://github.com/vmware-tanzu/tanzu-framework/tree/main/pkg/v1/sdk/capabilities/discovery)
provides methods to query a Kubernetes cluster for the state of its API surface.

`ClusterQueryClient` allows clients to build queries to inspect a cluster and evaluate results.

The sections below illustrate how to build a client and query for APIs and objects.

### Building a ClusterQueryClient

Use the constructor(s) from `discovery` package to get a query client.

```go
import (
"sigs.k8s.io/controller-runtime/pkg/client/config"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/sdk/capabilities/discovery"
)

cfg := config.GetConfig()

clusterQueryClient, err := discovery.NewClusterQueryClientForConfig(cfg)
if err != nil {
log.Error(err)
}
```

### Building and Executing Queries

Use `Group`, `Object` and `Schema` functions in the `discovery` package to build queries and execute them.

```go
import "github.com/vmware-tanzu/tanzu-framework/pkg/v1/sdk/capabilities/discovery"

// Define objects to query.
var pod = corev1.ObjectReference{
Kind: "Pod",
Name: "testpod",
Namespace: "testns",
APIVersion: "v1",
}

var testAnnotations = map[string]string{
"cluster.x-k8s.io/provider": "infrastructure-fake",
}

// Define queries.
var testObject = Object("podObj", &pod).WithAnnotations(testAnnotations)

var testGVR = Group("podResource", testapigroup.SchemeGroupVersion.Group).WithVersions("v1").WithResource("pods")

// Build query client.
c := clusterQueryClient.Query(testObject, testGVR)

// Execute returns combined result of all queries.
found, err := c.Execute()
if err != nil {
log.Error(err)
}

if found {
log.Info("Queries successful")
}

// Inspect granular results of each query using the Results method (should be called after Execute).
if result := c.Results().ForQuery("podResource"); result != nil {
if result.Found {
log.Info("Pod resource found")
} else {
log.Infof("Pod resource not found. Reason: %s", result.NotFoundReason)
}
}
```

## Executing Pre-defined TKG queries

The `pkg/v1/sdk/capabilities/discovery/tkg` package builds on top of the generic discovery package and exposes
pre-defined queries to determine a TKG cluster's capabilities.

Some examples are shown below.

```go
import tkgdiscovery "github.com/vmware-tanzu/tanzu-framework/pkg/v1/sdk/capabilities/discovery/tkg"

c, err := tkgdiscovery.NewDiscoveryClientForConfig(cfg)
if err != nil {
log.Fatal(err)
}

if c.IsTKGm() {
log.Info("This is a TKGm cluster")
}

if c.IsManagementCluster() {
log.Info("Management cluster")
}

if c.IsWorkloadCluster() {
log.Info("Workload cluster")
}

if c.HasCloudProvider(ctx, tkgdiscovery.CloudProviderVsphere) {
log.Info("Cluster has vSphere cloud provider")
}
```

## Capability CRD

Every TKG cluster starting from v1.4.0 includes a `Capability` CRD and an associated controller. Like the Go package
described above, a `Capability` CR can be used to craft queries to inspect a cluster's state and store the results the
CR's `status` field. `Capability` CRD's specification allows for different types of queries to inspect a cluster.

The full API can be found in [apis/run/v1alpha1/capability_types.go](../../apis/run/v1alpha1/capability_types.go)

### Example Capability Custom Resource

The following custom resource checks if the cluster is a TKG cluster which supports feature gating
abilities, and if it has NSX networking capabilities.

```yaml
apiVersion: run.tanzu.vmware.com/v1alpha1
kind: Capability
metadata:
name: tkg-capabilities
spec:
queries:
- name: "tanzu-cluster-with-feature-gating"
groupVersionResources:
- name: "tanzu-resource"
group: "run.tanzu.vmware.com"
versions:
- v1alpha1
resource: "tanzukubernetesreleases"
- name: "featuregate-resource"
group: "config.tanzu.vmware.com"
versions:
- v1alpha1
resource: "featuregates"
- name: "nsx-support"
objects:
- name: "nsx-namespace"
objectReference:
kind: "Namespace"
name: "vmware-system-nsx"
apiVersion: "v1"
```
The capabilities controller:
1. Watches `Capability` resources that are created or updated.
2. Executes queries specified in the spec.
3. Writes the results to the status field of the resource.

After reconciliation, results can be inspected by looking at the status field. Results are grouped by GVK, Object and
Partial Schema queries, and provide a predictable data structure for consumers to parse. They can be accessed by the
paths `status.results.groupVersionResources`, `status.results.objects` and `status.results.partialSchemas` respectively.

An example of query results is shown below.

```yaml
apiVersion: run.tanzu.vmware.com/v1alpha1
kind: Capability
metadata:
name: tkg-capabilities
spec:
# Omitted
status:
results:
- groupVersionResources:
- found: true
name: tanzu-resource
- found: true
name: featuregate-resource
name: tanzu-cluster-with-feature-gating
- name: nsx-support
objects:
- found: false
name: nsx-namespace
```
Loading

0 comments on commit 69c9425

Please sign in to comment.