Skip to content

Commit

Permalink
Add k8ssandra demo support to CLI (#1367)
Browse files Browse the repository at this point in the history
Summary: We want to add a K8ssandra demo to our list of demos. A few
changes needed to be made to ensure the demo can be deployed properly:
- We should check for cert-manager as a required dep. We decided not to
deploy this with the demo, since deploying 2 cert-managers can break
things. If we deploy cert-manager for the user, there's a bit of
bookkeeping we'd need to do if the user runs `px demo delete` to decide
whether or not we should also clean up cert-manager. Instead, we'll
leave this up to the user for the time-being.
- The demo deploys some CRDs. This can lead to race conditions if
deploying everything in the manifest at once. To handle this, added a
retry in the deploy block.

Relevant Issues: #680

Type of change: /kind cleanup

Test Plan: Push to dev demo manifest, `px demo deploy` with and without
cert-manager installed.

---------

Signed-off-by: Michelle Nguyen <[email protected]>
  • Loading branch information
aimichelle authored May 25, 2023
1 parent 87b7751 commit 12308c6
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
8 changes: 8 additions & 0 deletions demos/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ pkg_tar(
strip_prefix = "online-boutique",
)

pkg_tar(
name = "px-k8ssandra",
srcs = glob(["k8ssandra/*"]),
extension = "tar.gz",
strip_prefix = "k8ssandra",
)

ARCHIVES = [
":px-finagle",
":px-kafka",
":px-sock-shop",
":px-online-boutique",
":px-k8ssandra",
]

demo_upload(
Expand Down
7 changes: 7 additions & 0 deletions demos/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@
"Mux tracing is only enabled on newer kernels (>= 5.2) by default.",
"Make sure your system meets these requirements before deploying."
]
},
"px-k8ssandra": {
"description": "Microservice demo that spins up Cassandra and the Spring PetClinic demo app.",
"instructions": ["Use the px/cql_data script to view the Cassandra traffic flowing from the demo app."],
"dependencies": {
"cert-manager": true
}
}
}
1 change: 1 addition & 0 deletions src/pixie_cli/pkg/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ go_library(
"@com_github_alecthomas_chroma//quick",
"@com_github_blang_semver//:semver",
"@com_github_bmatcuk_doublestar//:doublestar",
"@com_github_cenkalti_backoff_v4//:backoff",
"@com_github_dustin_go_humanize//:go-humanize",
"@com_github_fatih_color//:color",
"@com_github_gofrs_uuid//:uuid",
Expand Down
54 changes: 49 additions & 5 deletions src/pixie_cli/pkg/cmd/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"strings"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/fatih/color"
"github.com/segmentio/analytics-go/v3"
log "github.com/sirupsen/logrus"
Expand All @@ -51,6 +52,7 @@ import (
const manifestFile = "manifest.json"

var errNamespaceAlreadyExists = errors.New("namespace already exists")
var errCertMgrDoesNotExist = errors.New("cert-manager does not exist")

func init() {
DemoCmd.PersistentFlags().String("artifacts", "https://storage.googleapis.com/pixie-prod-artifacts/prod-demo-apps", "The path to the demo apps")
Expand Down Expand Up @@ -324,11 +326,14 @@ func deployCmd(cmd *cobra.Command, args []string) {
return
}

err = setupDemoApp(appName, yamls)
err = setupDemoApp(appName, yamls, appSpec.Dependencies)
if err != nil {
if errors.Is(err, errNamespaceAlreadyExists) {
utils.Error("Failed to deploy demo application: namespace already exists.")
return
} else if errors.Is(err, errCertMgrDoesNotExist) {
utils.Error("Failed to deploy demo application: cert-manager needs to be installed. To deploy, please follow instructions at https://cert-manager.io/docs/getting-started/")
return
}
// Using log.Errorf rather than CLI log in order to track this unexpected error in Sentry.
log.WithError(err).Errorf("Error deploying demo application, deleting namespace %s", appName)
Expand All @@ -351,8 +356,9 @@ func deployCmd(cmd *cobra.Command, args []string) {
}

type manifestAppSpec struct {
Description string `json:"description"`
Instructions []string `json:"instructions"`
Description string `json:"description"`
Instructions []string `json:"instructions"`
Dependencies map[string]bool `json:"dependencies"`
}

type manifest = map[string]*manifestAppSpec
Expand Down Expand Up @@ -468,9 +474,40 @@ func createNamespace(namespace string) error {
return err
}

func setupDemoApp(appName string, yamls map[string][]byte) error {
func certManagerExists() (bool, error) {
kubeConfig := k8s.GetConfig()
clientset := k8s.GetClientset(kubeConfig)

deps, err := clientset.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{})
if err != nil {
return false, err
}

for _, d := range deps.Items {
if d.Name == "cert-manager" {
return true, nil
}
}

return false, err
}

func setupDemoApp(appName string, yamls map[string][]byte, deps map[string]bool) error {
kubeConfig := k8s.GetConfig()
clientset := k8s.GetClientset(kubeConfig)

// Check deps.
if deps["cert-manager"] {
certMgrExists, err := certManagerExists()
if err != nil && !k8s_errors.IsNotFound(err) {
return err
}

if !certMgrExists || k8s_errors.IsNotFound(err) {
return errCertMgrDoesNotExist
}
}

if namespaceExists(appName) {
fmt.Printf("%s: namespace %s already exists. If created with px, run %s to remove\n",
color.RedString("Error"), color.RedString(appName), color.GreenString(fmt.Sprintf("px demo delete %s", appName)))
Expand All @@ -484,7 +521,14 @@ func setupDemoApp(appName string, yamls map[string][]byte) error {
newTaskWrapper(fmt.Sprintf("Deploying %s YAMLs", appName), func() error {
for _, yamlBytes := range yamls {
yamlBytes := yamlBytes
err := k8s.ApplyYAML(clientset, kubeConfig, appName, bytes.NewReader(yamlBytes), false)
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 5 * time.Minute

op := func() error {
return k8s.ApplyYAML(clientset, kubeConfig, appName, bytes.NewReader(yamlBytes), false)
}

err := backoff.Retry(op, bo)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/shared/k8s/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func ApplyResources(clientset kubernetes.Interface, config *rest.Config, resourc
nsRes := res.Namespace(objNS)

createRes := nsRes
if k8sRes == "namespaces" || k8sRes == "configmap" || k8sRes == "clusterrolebindings" || k8sRes == "clusterroles" || k8sRes == "customresourcedefinitions" {
if k8sRes == "validatingwebhookconfigurations" || k8sRes == "mutatingwebhookconfigurations" || k8sRes == "namespaces" || k8sRes == "configmap" || k8sRes == "clusterrolebindings" || k8sRes == "clusterroles" || k8sRes == "customresourcedefinitions" {
createRes = res
}

Expand Down

0 comments on commit 12308c6

Please sign in to comment.