Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignored namespace option #1174

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/common"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/common/utils"

"github.com/odigos-io/odigos/cli/cmd/resources"
"github.com/odigos-io/odigos/cli/pkg/kube"
Expand All @@ -24,15 +25,14 @@ import (
)

var (
odigosCloudApiKeyFlag string
odigosOnPremToken string
namespaceFlag string
versionFlag string
skipWait bool
telemetryEnabled bool
psp bool
ignoredNamespaces []string
DefaultIgnoredNamespaces = []string{"odigos-system", "kube-system", "local-path-storage", "istio-system", "linkerd", "kube-node-lease"}
odigosCloudApiKeyFlag string
odigosOnPremToken string
namespaceFlag string
versionFlag string
skipWait bool
telemetryEnabled bool
psp bool
userInputIgnoredNamespaces []string

instrumentorImage string
odigletImage string
Expand All @@ -56,7 +56,6 @@ This command will install k8s components that will auto-instrument your applicat
}
ctx := cmd.Context()
ns := cmd.Flag("namespace").Value.String()
cmd.Flags().StringSliceVar(&ignoredNamespaces, "ignore-namespace", DefaultIgnoredNamespaces, "--ignore-namespace foo logging")

// Check if Odigos already installed
cm, err := client.CoreV1().ConfigMaps(ns).Get(ctx, resources.OdigosDeploymentConfigMapName, metav1.GetOptions{})
Expand Down Expand Up @@ -165,11 +164,13 @@ func createNamespace(ctx context.Context, cmd *cobra.Command, client *kube.Clien

func createOdigosConfigSpec() odigosv1.OdigosConfigurationSpec {

fullIgnoredNamespaces := utils.AddSystemNamespacesToIgnored(userInputIgnoredNamespaces, consts.SystemNamespaces)

return odigosv1.OdigosConfigurationSpec{
OdigosVersion: versionFlag,
ConfigVersion: 1, // config version starts at 1 and incremented on every config change
TelemetryEnabled: telemetryEnabled,
IgnoredNamespaces: ignoredNamespaces,
IgnoredNamespaces: fullIgnoredNamespaces,
Psp: psp,
ImagePrefix: imagePrefix,
OdigletImage: odigletImage,
Expand Down Expand Up @@ -200,6 +201,7 @@ func init() {
installCmd.Flags().StringVar(&autoScalerImage, "autoscaler-image", "keyval/odigos-autoscaler", "autoscaler container image name")
installCmd.Flags().StringVar(&imagePrefix, "image-prefix", "", "prefix for all container images. used when your cluster doesn't have access to docker hub")
installCmd.Flags().BoolVar(&psp, "psp", false, "enable pod security policy")
installCmd.Flags().StringSliceVar(&userInputIgnoredNamespaces, "ignore-namespace", consts.SystemNamespaces, "namespaces not to show in odigos ui")

if OdigosVersion != "" {
versionFlag = OdigosVersion
Expand Down
5 changes: 5 additions & 0 deletions cli/cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/odigos-io/odigos/cli/cmd/resources/odigospro"
"github.com/odigos-io/odigos/cli/pkg/confirm"
"github.com/odigos-io/odigos/cli/pkg/kube"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/common/utils"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -99,6 +101,9 @@ and apply any required migrations and adaptations.`,
config.Spec.OdigosVersion = versionFlag
config.Spec.ConfigVersion += 1

// make sure the current system namespaces is in the ignored in config
config.Spec.IgnoredNamespaces = utils.AddSystemNamespacesToIgnored(config.Spec.IgnoredNamespaces, consts.SystemNamespaces)

currentTier, err := odigospro.GetCurrentOdigosTier(ctx, client, ns)
if err != nil {
fmt.Println("Odigos cloud login failed - unable to read the current Odigos tier.")
Expand Down
8 changes: 7 additions & 1 deletion common/consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package consts

import "errors"
import (
"errors"
)

const (
CurrentNamespaceEnvVar = "CURRENT_NS"
Expand All @@ -23,3 +25,7 @@ const (
var (
PodsNotFoundErr = errors.New("could not find a ready pod")
)

var (
SystemNamespaces = []string{DefaultNamespace, "kube-system", "local-path-storage", "istio-system", "linkerd", "kube-node-lease"}
)
33 changes: 33 additions & 0 deletions common/utils/ignoredns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

func arrayContainsString(arr []string, str string) bool {
for _, elem := range arr {
if elem == str {
return true
}
}
return false
}

func AddSystemNamespacesToIgnored(userIgnoredNamespaces []string, systemNamespaces []string) []string {

mergedList := make([]string, len(userIgnoredNamespaces))
copy(mergedList, userIgnoredNamespaces)

for _, ns := range systemNamespaces {
if !arrayContainsString(mergedList, ns) {
mergedList = append(mergedList, ns)
}
}

return mergedList
}

func IsNamespaceIgnored(namespace string, ignoredNamespaces []string) bool {
for _, ignoredNamespace := range ignoredNamespaces {
if namespace == ignoredNamespace {
return true
}
}
return false
}
1 change: 1 addition & 0 deletions docs/cli/odigos_install.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ odigos install [flags]
--autoscaler-image autoscaler container image name
--image-prefix prefix for collector images. Used when your cluster doesn't have access to docker hub.
--psp enable pod security policy
--ignore-namespace namespaces not to show in odigos ui
```

The `--api-key` is required if you use Odigos Cloud.
Expand Down
6 changes: 0 additions & 6 deletions frontend/endpoints/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ func GetApplicationsInNamespace(c *gin.Context) {
return
}

if IsSystemNamespace(request.Namespace) {
// skip system namespaces which should not be instrumented
c.JSON(http.StatusOK, GetApplicationsInNamespaceResponse{})
return
}

ctx := c.Request.Context()
deps, err := getDeployments(request.Namespace, ctx)
if err != nil {
Expand Down
18 changes: 10 additions & 8 deletions frontend/endpoints/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go.uber.org/multierr"

"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/common/utils"

"github.com/odigos-io/odigos/frontend/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -26,7 +27,14 @@ type GetNamespaceItem struct {
TotalApps int `json:"totalApps"`
}

func GetNamespaces(c *gin.Context) {
func GetNamespaces(c *gin.Context, odigosns string) {

odigosConfig, err := kube.DefaultClient.OdigosClient.OdigosConfigurations(odigosns).Get(c.Request.Context(), "odigos-config", metav1.GetOptions{})
if err != nil {
returnError(c, err)
return
}

list, err := kube.DefaultClient.CoreV1().Namespaces().List(c.Request.Context(), metav1.ListOptions{})
if err != nil {
returnError(c, err)
Expand All @@ -41,9 +49,7 @@ func GetNamespaces(c *gin.Context) {

var response GetNamespacesResponse
for _, namespace := range list.Items {

if IsSystemNamespace(namespace.Name) {
// skip system namespaces which should not be instrumented
if utils.IsNamespaceIgnored(namespace.Name, odigosConfig.Spec.IgnoredNamespaces) {
continue
}

Expand Down Expand Up @@ -81,10 +87,6 @@ func PersistNamespaces(c *gin.Context) {
}

for nsName, nsItem := range request {
if IsSystemNamespace(nsName) {
// skip system namespaces which should not be instrumented
continue
}

jsonMergePayload := getJsonMergePatchForInstrumentationLabel(nsItem.FutureSelected)
_, err := kube.DefaultClient.CoreV1().Namespaces().Patch(c.Request.Context(), nsName, types.MergePatchType, jsonMergePayload, metav1.PatchOptions{})
Expand Down
11 changes: 0 additions & 11 deletions frontend/endpoints/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"path"

"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/frontend/kube"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -17,16 +16,6 @@ func GetImageURL(image string) string {
return path.Join(cdnUrl, image)
}

// TODO: read this from the odigosconfig CRD
func IsSystemNamespace(namespace string) bool {
return namespace == "kube-system" ||
namespace == consts.DefaultNamespace ||
namespace == "local-path-storage" ||
namespace == "istio-system" ||
namespace == "linkerd" ||
namespace == "kube-node-lease"
}

func setWorkloadInstrumentationLabel(ctx context.Context, nsName string, workloadName string, workloadKind WorkloadKind, enabled *bool) error {
jsonMergePatchData := getJsonMergePatchForInstrumentationLabel(enabled)

Expand Down
2 changes: 1 addition & 1 deletion frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func startHTTPServer(flags *Flags) (*gin.Engine, error) {
// Serve API
apis := r.Group("/api")
{
apis.GET("/namespaces", endpoints.GetNamespaces)
apis.GET("/namespaces", func(c *gin.Context) { endpoints.GetNamespaces(c, flags.Namespace) })
apis.POST("/namespaces", endpoints.PersistNamespaces)

apis.GET("/sources", endpoints.GetSources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { DropdownWrapper } from './sources.option.menu.styled';
import { KeyvalDropDown, KeyvalSearchInput, KeyvalText } from '@/design.system';
import { SETUP } from '@/utils/constants';

const DEFAULT_DROPDOWN_VALUE = { id: 0, label: 'default' };

export function FilterSourcesOptions({
setCurrentItem,
data,
searchFilter,
setSearchFilter,
currentNamespace,
}: any) {
function handleDropDownChange(item: any) {
setCurrentItem({ id: item?.id, name: item.label });
Expand All @@ -24,7 +23,7 @@ export function FilterSourcesOptions({
<DropdownWrapper>
<KeyvalText size={14}>{SETUP.MENU.NAMESPACES}</KeyvalText>
<KeyvalDropDown
value={DEFAULT_DROPDOWN_VALUE}
value={{ id: 0, label: currentNamespace.name }}
data={data}
onChange={handleDropDownChange}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function SourcesOptionMenu({
return (
<SourcesOptionMenuWrapper>
<FilterSourcesOptions
currentNamespace={currentNamespace}
setCurrentItem={setCurrentItem}
data={data}
searchFilter={searchFilter}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function SourcesSection({ sectionData, setSectionData }) {
if (!currentNamespace && data) {
const currentNamespace = data?.namespaces.find(
(item: Namespace) => item.name === DEFAULT
);
) ?? data?.namespaces[0];
setCurrentNamespace(currentNamespace);
}
}, [data]);
Expand Down
Loading