Skip to content

Commit

Permalink
feat: everything is a construct (#747) (#751)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `k8s-24/main` to `k8s-22/main`:
 - [feat: everything is a construct (#747)](#747)



### Questions ?
Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
  • Loading branch information
cdk8s-automation authored May 25, 2022
1 parent 1b66f76 commit 7e0a8e0
Show file tree
Hide file tree
Showing 21 changed files with 389 additions and 199 deletions.
4 changes: 2 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiObjectMetadata, ApiObject, ApiObjectMetadataDefinition } from 'cdk8s';
import { Construct } from 'constructs';
import { Construct, IConstruct } from 'constructs';
import { IApiResource, IApiEndpoint } from './api-resource.generated';

/**
Expand All @@ -16,7 +16,7 @@ export interface ResourceProps {
/**
* Represents a resource.
*/
export interface IResource {
export interface IResource extends IConstruct {
/**
* The Kubernetes name of this resource.
*/
Expand Down
37 changes: 30 additions & 7 deletions src/config-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,43 @@ export interface IConfigMap extends base.IResource {

}

class ImportedConfigMap extends Construct implements IConfigMap {

private readonly _name: string;

constructor(scope: Construct, id: string, name: string) {
super(scope, id);
this._name = name;
}

public get name(): string {
return this._name;
}

public get apiVersion(): string {
return k8s.KubeConfigMap.GVK.apiVersion;
}

public get apiGroup(): string {
return '';
}

public get kind(): string {
return k8s.KubeConfigMap.GVK.kind;
}

}

/**
* ConfigMap holds configuration data for pods to consume.
*/
export class ConfigMap extends base.Resource implements IConfigMap {

/**
* Represents a ConfigMap created elsewhere.
* @param name The name of the config map to import
*/
public static fromConfigMapName(name: string): IConfigMap {
return {
apiGroup: '',
name,
...k8s.KubeConfigMap.GVK,
};
public static fromConfigMapName(scope: Construct, id: string, name: string): IConfigMap {
return new ImportedConfigMap(scope, id, name);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiObject, Lazy } from 'cdk8s';
import { Construct } from 'constructs';
import { Construct, IConstruct } from 'constructs';
import * as base from './base';
import * as k8s from './imports/k8s';
import * as pod from './pod';
Expand All @@ -23,7 +23,7 @@ export interface NamespaceSelectorConfig {
/**
* Represents an object that can select namespaces.
*/
export interface INamespaceSelector {
export interface INamespaceSelector extends IConstruct {
/**
* Return the configuration of this selector.
*/
Expand Down Expand Up @@ -114,26 +114,28 @@ export interface NamespacesSelectOptions {
/**
* Represents a group of namespaces.
*/
export class Namespaces implements INamespaceSelector {
export class Namespaces extends Construct implements INamespaceSelector {

/**
* Select specific namespaces.
*/
public static select(options: NamespacesSelectOptions): Namespaces {
return new Namespaces(options.expressions, options.names, options.labels);
public static select(scope: Construct, id: string, options: NamespacesSelectOptions): Namespaces {
return new Namespaces(scope, id, options.expressions, options.names, options.labels);
}

/**
* Select all namespaces.
*/
public static all(): Namespaces {
return Namespaces.select({ expressions: [], labels: {} });
public static all(scope: Construct, id: string): Namespaces {
return Namespaces.select(scope, id, { expressions: [], labels: {} });
}

constructor(
constructor(scope: Construct, id: string,
private readonly expressions?: pod.LabelExpression[],
private readonly names?: string[],
private readonly labels?: { [key: string]: string }) { }
private readonly labels?: { [key: string]: string }) {
super(scope, id);
}

/**
* @see INamespaceSelector.toNamespaceSelectorConfig()
Expand Down
16 changes: 9 additions & 7 deletions src/pod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiObject, ApiObjectMetadataDefinition, Duration, Lazy, Names } from 'cdk8s';
import { Construct } from 'constructs';
import { Construct, IConstruct } from 'constructs';
import * as base from './base';
import * as container from './container';
import * as k8s from './imports/k8s';
Expand Down Expand Up @@ -450,7 +450,7 @@ export interface PodSelectorConfig {
/**
* Represents an object that can select pods.
*/
export interface IPodSelector {
export interface IPodSelector extends IConstruct {
/**
* Return the configuration of this selector.
*/
Expand Down Expand Up @@ -1061,19 +1061,21 @@ export interface PodSelectOptions {
/**
* Represents a group of pods.
*/
export class Pods implements IPodSelector {
export class Pods extends Construct implements IPodSelector {

/**
* Select pods in the cluster with various selectors.
*/
public static select(options: PodSelectOptions): Pods {
return new Pods(options.expressions, options.labels, options.namespaces);
public static select(scope: Construct, id: string, options: PodSelectOptions): Pods {
return new Pods(scope, id, options.expressions, options.labels, options.namespaces);
}

constructor(
constructor(scope: Construct, id: string,
private readonly expressions?: LabelExpression[],
private readonly labels?: { [key: string]: string },
private readonly namespaces?: namespace.INamespaceSelector) { }
private readonly namespaces?: namespace.INamespaceSelector) {
super(scope, id);
}

public toPodSelectorConfig(): PodSelectorConfig {
return {
Expand Down
38 changes: 30 additions & 8 deletions src/pv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ export interface PersistentVolumeProps extends base.ResourceProps {

}

class ImportedPersistentVolume extends Construct implements IPersistentVolume {

private readonly _name: string;

constructor(scope: Construct, id: string, name: string) {
super(scope, id);
this._name = name;
}

public get name(): string {
return this._name;
}

public get apiVersion(): string {
return k8s.KubePersistentVolume.GVK.apiVersion;
}

public get apiGroup(): string {
return '';
}

public get kind(): string {
return k8s.KubePersistentVolume.GVK.kind;
}

}

/**
* A PersistentVolume (PV) is a piece of storage in the cluster that has been
* provisioned by an administrator or dynamically provisioned using Storage Classes.
Expand All @@ -88,14 +115,9 @@ export class PersistentVolume extends base.Resource implements IPersistentVolume

/**
* Imports a pv from the cluster as a reference.
* @param volumeName The name of the pv to reference.
*/
public static fromPersistentVolumeName(volumeName: string): IPersistentVolume {
return {
apiGroup: '',
name: volumeName,
...k8s.KubePersistentVolume.GVK,
};
public static fromPersistentVolumeName(scope: Construct, id: string, volumeName: string): IPersistentVolume {
return new ImportedPersistentVolume(scope, id, volumeName);
}

/**
Expand Down Expand Up @@ -214,7 +236,7 @@ export class PersistentVolume extends base.Resource implements IPersistentVolume

public asVolume(): volume.Volume {
const claim = this.reserve();
return volume.Volume.fromPersistentVolumeClaim(claim);
return volume.Volume.fromPersistentVolumeClaim(this, 'Volume', claim);
}

/**
Expand Down
36 changes: 29 additions & 7 deletions src/pvc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ export interface PersistentVolumeClaimProps extends base.ResourceProps {

}

class ImportedPersistentVolumeClaim extends Construct implements IPersistentVolumeClaim {

private readonly _name: string;

constructor(scope: Construct, id: string, name: string) {
super(scope, id);
this._name = name;
}

public get name(): string {
return this._name;
}

public get apiVersion(): string {
return k8s.KubePersistentVolumeClaim.GVK.apiVersion;
}

public get apiGroup(): string {
return '';
}

public get kind(): string {
return k8s.KubePersistentVolumeClaim.GVK.kind;
}

}

/**
* A PersistentVolumeClaim (PVC) is a request for storage by a user.
* It is similar to a Pod. Pods consume node resources and PVCs consume PV resources.
Expand All @@ -81,14 +108,9 @@ export class PersistentVolumeClaim extends base.Resource implements IPersistentV

/**
* Imports a pvc from the cluster as a reference.
* @param claimName The name of the pvc to reference.
*/
public static fromClaimName(claimName: string): IPersistentVolumeClaim {
return {
apiGroup: '',
name: claimName,
...k8s.KubePersistentVolumeClaim.GVK,
};
public static fromClaimName(scope: Construct, id: string, claimName: string): IPersistentVolumeClaim {
return new ImportedPersistentVolumeClaim(scope, id, claimName);
}

/**
Expand Down
48 changes: 24 additions & 24 deletions src/role-binding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiObject, Lazy } from 'cdk8s';
import { Construct } from 'constructs';
import { Construct, IConstruct } from 'constructs';
import { Resource, ResourceProps } from './base';
import * as k8s from './imports/k8s';
import * as role from './role';
Expand All @@ -10,7 +10,7 @@ import { filterUndefined } from './utils';
* applies to. This can either hold a direct API object reference, or a value
* for non-objects such as user and group names.
*/
export interface ISubject {
export interface ISubject extends IConstruct {
/**
* APIGroup holds the API group of the referenced subject. Defaults to "" for
* ServiceAccount subjects. Defaults to "rbac.authorization.k8s.io" for User
Expand Down Expand Up @@ -183,45 +183,45 @@ export class ClusterRoleBinding extends Resource {
}

/**
* Properties for `User`.
* Represents a user.
*/
export interface UserProps {
export class User extends Construct implements ISubject {

/**
* The name of the user.
* Reference a user in the cluster by name.
*/
readonly name: string;
}
public static fromName(scope: Construct, id: string, name: string) {
return new User(scope, id, name);
}

/**
* Represents a user.
*/
export class User implements ISubject {
public readonly apiGroup: string | undefined = 'rbac.authorization.k8s.io';
public readonly kind: string = 'User';
public readonly name: string;
constructor(props: UserProps) {
this.name = props.name;

private constructor(scope: Construct, id: string, name: string) {
super(scope, id);
this.name = name;
}
}

/**
* Properties for `Group`.
* Represents a group.
*/
export interface GroupProps {
export class Group extends Construct implements ISubject {

/**
* The name of the group.
* Reference a group in the cluster by name.
*/
readonly name: string;
}
public static fromName(scope: Construct, id: string, name: string) {
return new Group(scope, id, name);
}

/**
* Represents a group.
*/
export class Group implements ISubject {
public readonly apiGroup: string | undefined = 'rbac.authorization.k8s.io';
public readonly kind: string = 'Group';
public readonly name: string;
constructor(props: GroupProps) {
this.name = props.name;

private constructor(scope: Construct, id: string, name: string) {
super(scope, id);
this.name = name;
}
}
Loading

0 comments on commit 7e0a8e0

Please sign in to comment.