|
| 1 | +package kube |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/postmanlabs/postman-insights-agent/cfg" |
| 9 | + "github.com/postmanlabs/postman-insights-agent/cmd/internal/cmderr" |
| 10 | + "github.com/postmanlabs/postman-insights-agent/rest" |
| 11 | + v1 "k8s.io/api/core/v1" |
| 12 | + |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "github.com/zclconf/go-cty/cty" |
| 15 | + "sigs.k8s.io/yaml" |
| 16 | + |
| 17 | + "github.com/hashicorp/hcl/v2/hclsyntax" |
| 18 | + "github.com/hashicorp/hcl/v2/hclwrite" |
| 19 | +) |
| 20 | + |
| 21 | +var printHelmChartFragmentCmd = &cobra.Command{ |
| 22 | + Use: "helm-fragment", |
| 23 | + Short: "Print a Helm chart container definition for adding the Postman Insights Agent to existing kubernetes deployment.", |
| 24 | + Long: "Print a container definition that can be inserted into a Helm Chart template to add the Postman Insights Agent as a sidecar container.", |
| 25 | + RunE: printHelmChartFragment, |
| 26 | + PersistentPreRun: kubeCommandPreRun, |
| 27 | +} |
| 28 | + |
| 29 | +var printTerraformFragmentCmd = &cobra.Command{ |
| 30 | + Use: "tf-fragment", |
| 31 | + Short: "Print an Terraform (HCL) code fragment for adding the Postman Insights Agent to an existing kubernetes deployment.", |
| 32 | + Long: "Print a Terraform (HCL) code fragment that can be inserted into a Terraform kubernetes_deployment resource spec to add the Postman Insights Agent as a sidecar container.", |
| 33 | + RunE: printTerraformFragment, |
| 34 | + PersistentPreRun: kubeCommandPreRun, |
| 35 | +} |
| 36 | + |
| 37 | +func printHelmChartFragment(_ *cobra.Command, _ []string) error { |
| 38 | + err := cmderr.CheckAPIKeyAndInsightsProjectID(insightsProjectID) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + // Create the Postman Insights Agent sidecar container |
| 44 | + container := createPostmanSidecar(insightsProjectID, false) |
| 45 | + // Store it in an array since the fragment will be added to a list of containers |
| 46 | + containerArray := []v1.Container{container} |
| 47 | + |
| 48 | + containerYamlBytes, err := yaml.Marshal(containerArray) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + containerYaml := indentCodeFragment(containerYamlBytes, 4) |
| 53 | + |
| 54 | + fmt.Printf("\n%s\n", containerYaml) |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func printTerraformFragment(_ *cobra.Command, _ []string) error { |
| 59 | + err := cmderr.CheckAPIKeyAndInsightsProjectID(insightsProjectID) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + // Create the Postman Insights Agent sidecar container |
| 65 | + hclBlockConfig := createTerraformContainer(insightsProjectID) |
| 66 | + hclBlockConfigString := indentCodeFragment(hclBlockConfig.Bytes(), 4) |
| 67 | + |
| 68 | + // Print the fragment |
| 69 | + fmt.Printf("\n%s\n", hclBlockConfigString) |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func createTerraformContainer(insightsProjectID string) *hclwrite.File { |
| 74 | + hclConfig := hclwrite.NewEmptyFile() |
| 75 | + rootBody := hclConfig.Body() |
| 76 | + |
| 77 | + rootBody.AppendUnstructuredTokens(hclwrite.Tokens{ |
| 78 | + { |
| 79 | + Type: hclsyntax.TokenComment, |
| 80 | + Bytes: []byte("# Add this fragment to your 'kubernetes_deployment' resource under 'spec.template.spec'. \n"), |
| 81 | + }, |
| 82 | + }) |
| 83 | + |
| 84 | + containerBlock := rootBody.AppendNewBlock("container", []string{}) |
| 85 | + containerBody := containerBlock.Body() |
| 86 | + |
| 87 | + containerBody.SetAttributeValue("name", cty.StringVal("postman-insights-agent")) |
| 88 | + containerBody.SetAttributeValue("image", cty.StringVal(akitaImage)) |
| 89 | + |
| 90 | + containerBody.AppendNewBlock("lifecycle", []string{}). |
| 91 | + Body().AppendNewBlock("pre_stop", []string{}). |
| 92 | + Body().AppendNewBlock("exec", []string{}). |
| 93 | + Body().SetAttributeValue("command", cty.ListVal([]cty.Value{ |
| 94 | + cty.StringVal("/bin/sh"), |
| 95 | + cty.StringVal("-c"), |
| 96 | + cty.StringVal("POSTMAN_INSIGHTS_AGENT_PID=$(pgrep postman-insights-agent) && kill -2 $POSTMAN_INSIGHTS_AGENT_PID && tail -f /proc/$POSTMAN_INSIGHTS_AGENT_PID/fd/1"), |
| 97 | + })) |
| 98 | + |
| 99 | + containerBody.AppendNewBlock("security_context", []string{}). |
| 100 | + Body().AppendNewBlock("capabilities", []string{}). |
| 101 | + Body().SetAttributeValue("add", cty.ListVal([]cty.Value{ |
| 102 | + cty.StringVal("NET_RAW"), |
| 103 | + })) |
| 104 | + |
| 105 | + // Add the args to the container |
| 106 | + args := cty.ListVal([]cty.Value{ |
| 107 | + cty.StringVal("apidump"), |
| 108 | + cty.StringVal("--project"), |
| 109 | + cty.StringVal(insightsProjectID), |
| 110 | + }) |
| 111 | + // If a non default --domain flag was used, specify it for the container as well. |
| 112 | + if rest.Domain != rest.DefaultDomain() { |
| 113 | + args.Add(cty.StringVal("--domain")) |
| 114 | + args.Add(cty.StringVal(rest.Domain)) |
| 115 | + } |
| 116 | + containerBody.SetAttributeValue("args", args) |
| 117 | + |
| 118 | + // Add the environment variables to the container |
| 119 | + pmKey, pmEnv := cfg.GetPostmanAPIKeyAndEnvironment() |
| 120 | + APIKeyEnvBlockBody := containerBody.AppendNewBlock("env", []string{}).Body() |
| 121 | + APIKeyEnvBlockBody.SetAttributeValue("name", cty.StringVal("POSTMAN_API_KEY")) |
| 122 | + APIKeyEnvBlockBody.SetAttributeValue("value", cty.StringVal(pmKey)) |
| 123 | + |
| 124 | + if pmEnv != "" { |
| 125 | + PostmanEnvBlockBody := containerBody.AppendNewBlock("env", []string{}).Body() |
| 126 | + PostmanEnvBlockBody.SetAttributeValue("name", cty.StringVal("POSTMAN_ENV")) |
| 127 | + PostmanEnvBlockBody.SetAttributeValue("value", cty.StringVal(pmEnv)) |
| 128 | + } |
| 129 | + |
| 130 | + return hclConfig |
| 131 | +} |
| 132 | + |
| 133 | +func indentCodeFragment(codeFragmentInBytes []byte, indentLevel int) string { |
| 134 | + // Trim off any extraneous newlines. |
| 135 | + codeFragmentInBytes = bytes.Trim(codeFragmentInBytes, "\n") |
| 136 | + |
| 137 | + // Indent level prefix |
| 138 | + indentPrefix := strings.Repeat(" ", indentLevel) |
| 139 | + |
| 140 | + indentedCodeFragment := indentPrefix + strings.ReplaceAll( |
| 141 | + string(codeFragmentInBytes), "\n", "\n"+indentPrefix) |
| 142 | + |
| 143 | + return indentedCodeFragment |
| 144 | +} |
| 145 | + |
| 146 | +func init() { |
| 147 | + Cmd.AddCommand(printHelmChartFragmentCmd) |
| 148 | + Cmd.AddCommand(printTerraformFragmentCmd) |
| 149 | +} |
0 commit comments