-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2307 from upalatucci/use-namspace-udn
CNV-51530: hide virtctl on managed udn namespaces
- Loading branch information
Showing
9 changed files
with
233 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { | ||
ClusterUserDefinedNetworkModelGroupVersionKind, | ||
modelToGroupVersionKind, | ||
ProjectModel, | ||
UserDefinedNetworkModelGroupVersionKind, | ||
} from '@kubevirt-utils/models'; | ||
import { | ||
ClusterUserDefinedNetworkKind, | ||
UserDefinedNetworkKind, | ||
UserDefinedNetworkRole, | ||
} from '@kubevirt-utils/resources/udn/types'; | ||
import { matchSelector } from '@kubevirt-utils/utils/matchSelector'; | ||
import { isEmpty } from '@kubevirt-utils/utils/utils'; | ||
import { K8sResourceCommon, useK8sWatchResource } from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
const useNamespaceUDN = ( | ||
namespace: string, | ||
): [ | ||
isNamespaceManagedByUDN: boolean, | ||
udn: ClusterUserDefinedNetworkKind | UserDefinedNetworkKind, | ||
] => { | ||
const [project] = useK8sWatchResource<K8sResourceCommon>({ | ||
groupVersionKind: modelToGroupVersionKind(ProjectModel), | ||
name: namespace, | ||
}); | ||
|
||
const [udns] = useK8sWatchResource<UserDefinedNetworkKind[]>({ | ||
groupVersionKind: UserDefinedNetworkModelGroupVersionKind, | ||
isList: true, | ||
namespace, | ||
}); | ||
|
||
const [clusterUDNs] = useK8sWatchResource<ClusterUserDefinedNetworkKind[]>({ | ||
groupVersionKind: ClusterUserDefinedNetworkModelGroupVersionKind, | ||
isList: true, | ||
}); | ||
|
||
const primaryUDN = udns?.find( | ||
(udn) => udn?.spec?.layer2?.role === UserDefinedNetworkRole.Primary, | ||
); | ||
|
||
const primaryClustersUDNs = clusterUDNs | ||
?.filter((clusterUDN) => matchSelector(project, clusterUDN?.spec?.namespaceSelector)) | ||
?.find((udn) => udn?.spec?.network?.layer2?.role === UserDefinedNetworkRole.Primary); | ||
|
||
const isNamespaceManagedByUDN = !isEmpty(primaryUDN || primaryClustersUDNs); | ||
|
||
return [isNamespaceManagedByUDN, primaryUDN || primaryClustersUDNs]; | ||
}; | ||
|
||
export default useNamespaceUDN; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { K8sResourceKind, Selector } from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
export type UserDefinedNetworkAnnotations = { | ||
description?: string; | ||
}; | ||
|
||
export enum UserDefinedNetworkRole { | ||
Primary = 'Primary', | ||
Secondary = 'Secondary', | ||
} | ||
|
||
export type UserDefinedNetworkLayer2 = { | ||
ipamLifecycle?: string; | ||
joinSubnets?: string[]; | ||
mtu?: number; | ||
role: UserDefinedNetworkRole; | ||
subnets?: string[]; | ||
}; | ||
|
||
export type UserDefinedNetworkLayer3Subnet = { | ||
cidr: string; | ||
hostSubnet?: number; | ||
}; | ||
|
||
export type UserDefinedNetworkSubnet = string | UserDefinedNetworkLayer3Subnet; | ||
|
||
export type UserDefinedNetworkLayer3 = { | ||
joinSubnets?: string[]; | ||
mtu?: number; | ||
role: string; | ||
subnets?: UserDefinedNetworkLayer3Subnet[]; | ||
}; | ||
|
||
export type ClusterUserDefinedNetworkSpec = { | ||
namespaceSelector?: Selector; | ||
network?: UserDefinedNetworkSpec; | ||
}; | ||
|
||
export type UserDefinedNetworkSpec = { | ||
layer2?: UserDefinedNetworkLayer2; | ||
layer3?: UserDefinedNetworkLayer3; | ||
topology: string; | ||
}; | ||
|
||
export type ClusterUserDefinedNetworkKind = { | ||
spec?: ClusterUserDefinedNetworkSpec; | ||
} & K8sResourceKind; | ||
|
||
export type UserDefinedNetworkKind = { | ||
spec?: UserDefinedNetworkSpec; | ||
} & K8sResourceKind; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
K8sResourceCommon, | ||
MatchExpression, | ||
Operator, | ||
Selector, | ||
} from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
import { isEmpty } from './utils'; | ||
|
||
export const createEquals = (key: string, value: string): MatchExpression => ({ | ||
key, | ||
operator: Operator.Equals, | ||
values: [value], | ||
}); | ||
|
||
export const matchExpressionSatisfied = ( | ||
expression: MatchExpression, | ||
labels: { [key in string]: string }, | ||
): boolean => { | ||
switch (expression.operator) { | ||
case Operator.Equals: | ||
return labels?.[expression.key] === expression.values?.[0]; | ||
case Operator.NotEqual: | ||
return labels?.[expression.key] !== expression.values?.[0]; | ||
case Operator.NotEquals: | ||
return labels?.[expression.key] !== expression.values?.[0]; | ||
case Operator.Exists: | ||
return !isEmpty(labels?.[expression.key]); | ||
case Operator.DoesNotExist: | ||
return isEmpty(labels?.[expression.key]); | ||
case Operator.GreaterThan: | ||
return labels?.[expression.key] > expression.values?.[0]; | ||
case Operator.LessThan: | ||
return labels?.[expression.key] < expression.values?.[0]; | ||
case Operator.In: | ||
return expression.values?.includes(labels?.[expression.key]); | ||
case Operator.NotIn: | ||
return !expression.values?.includes(labels?.[expression.key]); | ||
} | ||
}; | ||
|
||
export const matchSelector = (resource: K8sResourceCommon, selector: Selector) => { | ||
const { matchExpressions, matchLabels } = selector || {}; | ||
|
||
const requirements = Object.keys(matchLabels || {}) | ||
.sort() | ||
.map((matchLabel) => createEquals(matchLabel, matchLabels[matchLabel])); | ||
|
||
const allExpressions = [...requirements, ...(matchExpressions || [])]; | ||
|
||
return allExpressions.every((expression) => | ||
matchExpressionSatisfied(expression, resource?.metadata?.labels), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters