Skip to content

Commit

Permalink
refactor(core): make Provider type slightly more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Aug 12, 2020
1 parent d4b99e4 commit a49faf6
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 67 deletions.
6 changes: 3 additions & 3 deletions core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
"tslint-microsoft-contrib": "^6.2.0",
"tslint-no-unused": "^0.2.0-alpha.1",
"tslint-plugin-prettier": "^2.3.0",
"typescript": "^3.9.3"
"typescript": "^3.9.6"
},
"scripts": {
"add-version-files": "node ./build/src/bin/add-version-files.js",
Expand Down Expand Up @@ -253,4 +253,4 @@
]
},
"gitHead": "b0647221a4d2ff06952bae58000b104215aed922"
}
}
4 changes: 2 additions & 2 deletions core/src/config/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Joi from "@hapi/joi"
import chalk from "chalk"
import { isString, fromPairs, mapValues } from "lodash"
import { PrimitiveMap, joiIdentifierMap, joiStringMap, joiPrimitive, DeepPrimitiveMap, joiVariables } from "./common"
import { Provider, ProviderConfig, ProviderMap } from "./provider"
import { Provider, GenericProviderConfig, ProviderMap } from "./provider"
import { ConfigurationError } from "../exceptions"
import { resolveTemplateString } from "../template-string"
import { Garden } from "../garden"
Expand Down Expand Up @@ -530,7 +530,7 @@ class ProviderContext extends ConfigContext {
.example({ clusterHostname: "my-cluster.example.com" })
.meta({ keyPlaceholder: "<config-key>" })
)
public config: ProviderConfig
public config: GenericProviderConfig

@schema(
joiIdentifierMap(
Expand Down
8 changes: 4 additions & 4 deletions core/src/config/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { findByName, getNames } from "../util/util"
import { ConfigurationError, ParameterError, ValidationError } from "../exceptions"
import { PrimitiveMap } from "./common"
import { cloneDeep, omit, isPlainObject } from "lodash"
import { providerConfigBaseSchema, ProviderConfig } from "./provider"
import { providerConfigBaseSchema, GenericProviderConfig } from "./provider"
import { DEFAULT_API_VERSION, DOCS_BASE_URL } from "../constants"
import { defaultDotIgnoreFiles } from "../util/fs"
import { pathExists, readFile } from "fs-extra"
Expand All @@ -54,7 +54,7 @@ export interface ParsedEnvironment {
export interface EnvironmentConfig {
name: string
defaultNamespace: string | null
providers?: ProviderConfig[] // further validated by each plugin
providers?: GenericProviderConfig[] // further validated by each plugin
varfile?: string
variables: DeepPrimitiveMap
production?: boolean
Expand Down Expand Up @@ -207,7 +207,7 @@ export interface ProjectConfig {
exclude?: string[]
}
outputs?: OutputSpec[]
providers: ProviderConfig[]
providers: GenericProviderConfig[]
sources?: SourceConfig[]
varfile?: string
variables: DeepPrimitiveMap
Expand Down Expand Up @@ -574,7 +574,7 @@ export async function pickEnvironment({
...envProviders,
]

const mergedProviders: { [name: string]: ProviderConfig } = {}
const mergedProviders: { [name: string]: GenericProviderConfig } = {}

for (const provider of allProviders) {
if (!!mergedProviders[provider.name]) {
Expand Down
15 changes: 9 additions & 6 deletions core/src/config/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import { EnvironmentStatus } from "../types/plugin/provider/getEnvironmentStatus
import { environmentStatusSchema } from "./status"
import { PluginTools } from "../types/plugin/tools"

export interface ProviderConfig {
export interface BaseProviderConfig {
name: string
environments?: string[]
}

export interface GenericProviderConfig extends BaseProviderConfig {
[key: string]: any
}

Expand Down Expand Up @@ -48,7 +51,7 @@ export const providerConfigBaseSchema = () =>
.meta({ extendable: true })
.id("providerConfig")

export interface Provider<T extends ProviderConfig = ProviderConfig> {
export interface Provider<T extends BaseProviderConfig = BaseProviderConfig> {
name: string
dependencies: { [name: string]: Provider }
environments?: string[]
Expand Down Expand Up @@ -78,7 +81,7 @@ export const providerSchema = () =>
.id("provider")

export interface ProviderMap {
[name: string]: Provider
[name: string]: Provider<GenericProviderConfig>
}

export const defaultProviders = [{ name: "container" }]
Expand All @@ -94,7 +97,7 @@ export const defaultProvider: Provider = {
}

export function providerFromConfig(
config: ProviderConfig,
config: GenericProviderConfig,
dependencies: ProviderMap,
moduleConfigs: ModuleConfig[],
status: EnvironmentStatus,
Expand All @@ -114,14 +117,14 @@ export function providerFromConfig(
* Given a plugin and its provider config, return a list of dependency names based on declared dependencies,
* as well as implicit dependencies based on template strings.
*/
export async function getAllProviderDependencyNames(plugin: GardenPlugin, config: ProviderConfig) {
export async function getAllProviderDependencyNames(plugin: GardenPlugin, config: GenericProviderConfig) {
return uniq([...(plugin.dependencies || []), ...(await getProviderTemplateReferences(config))]).sort()
}

/**
* Given a provider config, return implicit dependencies based on template strings.
*/
export async function getProviderTemplateReferences(config: ProviderConfig) {
export async function getProviderTemplateReferences(config: GenericProviderConfig) {
const references = collectTemplateReferences(config)
const deps: string[] = []

Expand Down
8 changes: 4 additions & 4 deletions core/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
} from "./util/fs"
import {
Provider,
ProviderConfig,
GenericProviderConfig,
getAllProviderDependencyNames,
defaultProvider,
ProviderMap,
Expand Down Expand Up @@ -137,7 +137,7 @@ export interface GardenParams {
projectName: string
projectRoot: string
projectSources?: SourceConfig[]
providerConfigs: ProviderConfig[]
providerConfigs: GenericProviderConfig[]
variables: DeepPrimitiveMap
secrets: StringMap
sessionId: string | null
Expand Down Expand Up @@ -184,7 +184,7 @@ export class Garden {
public readonly gardenDirPath: string
public readonly artifactsPath: string
public readonly opts: GardenOpts
private readonly providerConfigs: ProviderConfig[]
private readonly providerConfigs: GenericProviderConfig[]
public readonly workingCopyId: string
public readonly dotIgnoreFiles: string[]
public readonly moduleIncludePatterns?: string[]
Expand Down Expand Up @@ -1203,7 +1203,7 @@ export interface ConfigDump {
environmentName: string // TODO: Remove this?
allEnvironmentNames: string[]
namespace: string
providers: (Omit<Provider, "tools"> | ProviderConfig)[]
providers: (Omit<Provider, "tools"> | GenericProviderConfig)[]
variables: DeepPrimitiveMap
moduleConfigs: ModuleConfig[]
workflowConfigs: WorkflowConfig[]
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Garden } from "./garden"
import { cloneDeep } from "lodash"
import { projectNameSchema, projectSourcesSchema, environmentNameSchema } from "./config/project"
import { Provider, providerSchema, ProviderConfig } from "./config/provider"
import { Provider, providerSchema, GenericProviderConfig } from "./config/provider"
import { deline } from "./util/string"
import { joi, joiVariables, PrimitiveMap } from "./config/common"

Expand All @@ -31,7 +31,7 @@ export interface CommandInfo {
opts: PrimitiveMap
}

export interface PluginContext<C extends ProviderConfig = ProviderConfig> extends WrappedFromGarden {
export interface PluginContext<C extends GenericProviderConfig = GenericProviderConfig> extends WrappedFromGarden {
command?: CommandInfo
provider: Provider<C>
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
pluginSchema,
ModuleTypeMap,
} from "./types/plugin/plugin"
import { ProviderConfig } from "./config/provider"
import { GenericProviderConfig } from "./config/provider"
import { ConfigurationError, PluginError, RuntimeError } from "./exceptions"
import { uniq, mapValues, fromPairs, flatten, keyBy, some } from "lodash"
import { findByName, pushToKey, getNames } from "./util/util"
Expand All @@ -23,7 +23,7 @@ import { validateSchema } from "./config/validation"
import { LogEntry } from "./logger/log-entry"
import { DependencyValidationGraph } from "./util/validate-dependencies"

export function loadPlugins(log: LogEntry, registeredPlugins: PluginMap, configs: ProviderConfig[]) {
export function loadPlugins(log: LogEntry, registeredPlugins: PluginMap, configs: GenericProviderConfig[]) {
const loadedPlugins: PluginMap = {}

const loadPlugin = (name: string) => {
Expand Down Expand Up @@ -107,7 +107,7 @@ export function loadPlugins(log: LogEntry, registeredPlugins: PluginMap, configs
/**
* Returns the given provider configs in dependency order.
*/
export function getDependencyOrder<T extends ProviderConfig>(configs: T[], registeredPlugins: PluginMap): T[] {
export function getDependencyOrder<T extends GenericProviderConfig>(configs: T[], registeredPlugins: PluginMap): T[] {
const graph = new DependencyValidationGraph()

for (const plugin of Object.values(registeredPlugins)) {
Expand Down Expand Up @@ -141,7 +141,7 @@ export function getDependencyOrder<T extends ProviderConfig>(configs: T[], regis
}

// Takes a plugin and resolves it against its base plugin, if applicable
function resolvePlugin(plugin: GardenPlugin, loadedPlugins: PluginMap, configs: ProviderConfig[]): GardenPlugin {
function resolvePlugin(plugin: GardenPlugin, loadedPlugins: PluginMap, configs: GenericProviderConfig[]): GardenPlugin {
if (!plugin.base) {
return plugin
}
Expand Down Expand Up @@ -320,7 +320,7 @@ interface ModuleDefinitionMap {
[moduleType: string]: { plugin: GardenPlugin; spec: ModuleTypeDefinition }
}

function resolveModuleDefinitions(resolvedPlugins: PluginMap, configs: ProviderConfig[]): PluginMap {
function resolveModuleDefinitions(resolvedPlugins: PluginMap, configs: GenericProviderConfig[]): PluginMap {
// Collect module type declarations
const graph = new DependencyValidationGraph()
const moduleDefinitionMap: { [moduleType: string]: { plugin: GardenPlugin; spec: ModuleTypeDefinition }[] } = {}
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/conftest/conftest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { resolve, relative } from "path"
import { createGardenPlugin } from "../../types/plugin/plugin"
import { providerConfigBaseSchema, ProviderConfig, Provider } from "../../config/provider"
import { providerConfigBaseSchema, GenericProviderConfig, Provider } from "../../config/provider"
import { joi, joiIdentifier, joiArray } from "../../config/common"
import { dedent, naturalList } from "../../util/string"
import { TestModuleParams } from "../../types/plugin/module/testModule"
Expand All @@ -25,7 +25,7 @@ import { getK8sProvider } from "../openfaas/config"
import { renderTemplates } from "../kubernetes/helm/common"
import { LogEntry } from "../../logger/log-entry"

export interface ConftestProviderConfig extends ProviderConfig {
export interface ConftestProviderConfig extends GenericProviderConfig {
policyPath: string
namespace?: string
testFailureThreshold: "deny" | "warn" | "none"
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import { SuggestModulesParams, SuggestModulesResult } from "../../types/plugin/m
import { listDirectory } from "../../util/fs"
import { dedent } from "../../util/string"
import { getModuleTypeUrl } from "../../docs/common"
import { Provider, ProviderConfig } from "../../config/provider"
import { Provider, GenericProviderConfig } from "../../config/provider"

export interface ContainerProviderConfig extends ProviderConfig {}
export interface ContainerProviderConfig extends GenericProviderConfig {}
export type ContainerProvider = Provider<ContainerProviderConfig>

export const containerModuleOutputsSchema = () =>
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/google/google-cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type GcfServiceSpec = GcfModuleSpec

export interface GcfModule extends GardenModule<GcfModuleSpec, GcfServiceSpec, ExecTestSpec> {}

function getGcfProject<T extends GcfModule>(service: Service<T>, provider: Provider) {
function getGcfProject<T extends GcfModule>(service: Service<T>, provider: Provider<any>) {
return service.spec.project || provider.config.defaultProject || null
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/plugins/hadolint/hadolint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import Bluebird from "bluebird"
import { join, relative, resolve } from "path"
import { pathExists, readFile } from "fs-extra"
import { createGardenPlugin, GardenModule } from "../../sdk"
import { providerConfigBaseSchema, ProviderConfig, Provider } from "../../config/provider"
import { providerConfigBaseSchema, GenericProviderConfig, Provider } from "../../config/provider"
import { joi } from "../../config/common"
import { dedent, splitLines, naturalList } from "../../util/string"
import { TestModuleParams } from "../../types/plugin/module/testModule"
import { STATIC_DIR } from "../../constants"
import { padStart, padEnd } from "lodash"
import chalk from "chalk"
import { ConfigurationError } from "../../exceptions"
import { containerHelpers } from "../container/helpers"
import { baseBuildSpecSchema } from "../../config/module"
import { getProviderUrl, getModuleTypeUrl, getGitHubUrl } from "../../docs/common"
import { TestModuleParams } from "../../types/plugin/module/testModule"

const defaultConfigPath = join(STATIC_DIR, "hadolint", "default.hadolint.yaml")
const configFilename = ".hadolint.yaml"

interface HadolintProviderConfig extends ProviderConfig {
interface HadolintProviderConfig extends GenericProviderConfig {
autoInject: boolean
testFailureThreshold: "error" | "warning" | "none"
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import dedent = require("dedent")

import { joiArray, joiIdentifier, joiProviderName, joi, joiStringMap } from "../../config/common"
import { Provider, providerConfigBaseSchema, ProviderConfig } from "../../config/provider"
import { Provider, providerConfigBaseSchema, GenericProviderConfig } from "../../config/provider"
import {
containerRegistryConfigSchema,
ContainerRegistryConfig,
Expand Down Expand Up @@ -87,7 +87,7 @@ export type ContainerBuildMode = "local-docker" | "cluster-docker" | "kaniko"
export type DefaultDeploymentStrategy = "rolling"
export type DeploymentStrategy = DefaultDeploymentStrategy | "blue-green"

export interface KubernetesConfig extends ProviderConfig {
export interface KubernetesConfig extends GenericProviderConfig {
buildMode: ContainerBuildMode
clusterDocker?: {
enableBuildKit?: boolean
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/openfaas/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Service } from "../../types/service"
import { ExecModuleSpecBase, ExecTestSpec } from "../exec"
import { KubernetesProvider } from "../kubernetes/config"
import { CommonServiceSpec } from "../../config/service"
import { Provider, providerConfigBaseSchema, ProviderConfig, ProviderMap } from "../../config/provider"
import { Provider, providerConfigBaseSchema, GenericProviderConfig, ProviderMap } from "../../config/provider"
import { union } from "lodash"
import { ContainerModule } from "../container/config"
import { ConfigureModuleParams, ConfigureModuleResult } from "../../types/plugin/module/configure"
Expand Down Expand Up @@ -83,7 +83,7 @@ export interface OpenFaasModule extends GardenModule<OpenFaasModuleSpec, CommonS
export type OpenFaasModuleConfig = OpenFaasModule["_config"]
export interface OpenFaasService extends Service<OpenFaasModule> {}

export interface OpenFaasConfig extends ProviderConfig {
export interface OpenFaasConfig extends GenericProviderConfig {
gatewayUrl: string
hostname: string
faasNetes: {
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/terraform/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { join } from "path"
import { pathExists } from "fs-extra"
import { createGardenPlugin } from "../../types/plugin/plugin"
import { getEnvironmentStatus, prepareEnvironment } from "./init"
import { providerConfigBaseSchema, ProviderConfig, Provider } from "../../config/provider"
import { providerConfigBaseSchema, GenericProviderConfig, Provider } from "../../config/provider"
import { joi, joiVariables } from "../../config/common"
import { dedent } from "../../util/string"
import { supportedVersions, defaultTerraformVersion, terraformCliSpecs } from "./cli"
Expand All @@ -23,7 +23,7 @@ import { SuggestModulesParams, SuggestModulesResult } from "../../types/plugin/m
import { listDirectory } from "../../util/fs"
import { terraformCommands } from "./commands"

type TerraformProviderConfig = ProviderConfig &
type TerraformProviderConfig = GenericProviderConfig &
TerraformBaseSpec & {
initRoot?: string
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

export { createGardenPlugin } from "../types/plugin/plugin"
export { GardenModule } from "../types/module"
export { ProviderConfig, Provider } from "../config/provider"
export { GenericProviderConfig, Provider } from "../config/provider"
Loading

0 comments on commit a49faf6

Please sign in to comment.