mirror of https://github.com/harness/drone.git
feat: [CDE-115]: Adding infra provider factory (#2138)
parent
f53480a8ad
commit
06a514976f
|
@ -28,7 +28,7 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ InfraProvider = (*dockerProvider)(nil)
|
var _ InfraProvider = (*DockerProvider)(nil)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
DockerHost string
|
DockerHost string
|
||||||
|
@ -37,19 +37,19 @@ type Config struct {
|
||||||
DockerTLSVerify string
|
DockerTLSVerify string
|
||||||
}
|
}
|
||||||
|
|
||||||
type dockerProvider struct {
|
type DockerProvider struct {
|
||||||
config *Config
|
config *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerProvider(config *Config) InfraProvider {
|
func NewDockerProvider(config *Config) *DockerProvider {
|
||||||
return &dockerProvider{
|
return &DockerProvider{
|
||||||
config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provision assumes a docker engine is already running on the Gitness host machine and re-uses that as infra.
|
// Provision assumes a docker engine is already running on the Gitness host machine and re-uses that as infra.
|
||||||
// It does not start docker engine.
|
// It does not start docker engine.
|
||||||
func (d dockerProvider) Provision(ctx context.Context, _ string, params []Parameter) (Infrastructure, error) {
|
func (d DockerProvider) Provision(ctx context.Context, _ string, params []Parameter) (Infrastructure, error) {
|
||||||
dockerClient, closeFunc, err := d.getClient(params)
|
dockerClient, closeFunc, err := d.getClient(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Infrastructure{}, err
|
return Infrastructure{}, err
|
||||||
|
@ -66,53 +66,53 @@ func (d dockerProvider) Provision(ctx context.Context, _ string, params []Parame
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dockerProvider) Find(_ context.Context, _ string, _ []Parameter) (Infrastructure, error) {
|
func (d DockerProvider) Find(_ context.Context, _ string, _ []Parameter) (Infrastructure, error) {
|
||||||
// TODO implement me
|
// TODO implement me
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop is NOOP as this provider uses already running docker engine. It does not stop the docker engine.
|
// Stop is NOOP as this provider uses already running docker engine. It does not stop the docker engine.
|
||||||
func (d dockerProvider) Stop(_ context.Context, infra Infrastructure) (Infrastructure, error) {
|
func (d DockerProvider) Stop(_ context.Context, infra Infrastructure) (Infrastructure, error) {
|
||||||
return infra, nil
|
return infra, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy is NOOP as this provider uses already running docker engine. It does not stop the docker engine.
|
// Destroy is NOOP as this provider uses already running docker engine. It does not stop the docker engine.
|
||||||
func (d dockerProvider) Destroy(_ context.Context, infra Infrastructure) (Infrastructure, error) {
|
func (d DockerProvider) Destroy(_ context.Context, infra Infrastructure) (Infrastructure, error) {
|
||||||
return infra, nil
|
return infra, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dockerProvider) Status(_ context.Context, _ Infrastructure) (enum.InfraStatus, error) {
|
func (d DockerProvider) Status(_ context.Context, _ Infrastructure) (enum.InfraStatus, error) {
|
||||||
// TODO implement me
|
// TODO implement me
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AvailableParams returns empty slice as no params are defined.
|
// AvailableParams returns empty slice as no params are defined.
|
||||||
func (d dockerProvider) AvailableParams() []ParameterSchema {
|
func (d DockerProvider) AvailableParams() []ParameterSchema {
|
||||||
return []ParameterSchema{}
|
return []ParameterSchema{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateParams returns nil as no params are defined.
|
// ValidateParams returns nil as no params are defined.
|
||||||
func (d dockerProvider) ValidateParams(_ []Parameter) error {
|
func (d DockerProvider) ValidateParams(_ []Parameter) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TemplateParams returns nil as no template params are used.
|
// TemplateParams returns nil as no template params are used.
|
||||||
func (d dockerProvider) TemplateParams() []ParameterSchema {
|
func (d DockerProvider) TemplateParams() []ParameterSchema {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProvisioningType returns existing as docker provider doesn't create new resources.
|
// ProvisioningType returns existing as docker provider doesn't create new resources.
|
||||||
func (d dockerProvider) ProvisioningType() enum.InfraProvisioningType {
|
func (d DockerProvider) ProvisioningType() enum.InfraProvisioningType {
|
||||||
return enum.InfraProvisioningTypeExisting
|
return enum.InfraProvisioningTypeExisting
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dockerProvider) Exec(_ context.Context, _ Infrastructure, _ []string) (io.Reader, io.Reader, error) {
|
func (d DockerProvider) Exec(_ context.Context, _ Infrastructure, _ []string) (io.Reader, io.Reader, error) {
|
||||||
// TODO implement me
|
// TODO implement me
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client returns a new docker client created using params.
|
// Client returns a new docker client created using params.
|
||||||
func (d dockerProvider) Client(_ context.Context, infra Infrastructure) (Client, error) {
|
func (d DockerProvider) Client(_ context.Context, infra Infrastructure) (Client, error) {
|
||||||
dockerClient, closeFunc, err := d.getClient(infra.Parameters)
|
dockerClient, closeFunc, err := d.getClient(infra.Parameters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -124,7 +124,7 @@ func (d dockerProvider) Client(_ context.Context, infra Infrastructure) (Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
// getClient returns a new docker client created using values from gitness docker config.
|
// getClient returns a new docker client created using values from gitness docker config.
|
||||||
func (d dockerProvider) getClient(_ []Parameter) (*client.Client, func(context.Context), error) {
|
func (d DockerProvider) getClient(_ []Parameter) (*client.Client, func(context.Context), error) {
|
||||||
var opts []client.Opt
|
var opts []client.Opt
|
||||||
|
|
||||||
opts = append(opts, client.WithHost(d.config.DockerHost))
|
opts = append(opts, client.WithHost(d.config.DockerHost))
|
||||||
|
@ -154,7 +154,7 @@ func (d dockerProvider) getClient(_ []Parameter) (*client.Client, func(context.C
|
||||||
return dockerClient, closeFunc, nil
|
return dockerClient, closeFunc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dockerProvider) getHTTPSClient() (*http.Client, error) {
|
func (d DockerProvider) getHTTPSClient() (*http.Client, error) {
|
||||||
options := tlsconfig.Options{
|
options := tlsconfig.Options{
|
||||||
CAFile: filepath.Join(d.config.DockerCertPath, "ca.pem"),
|
CAFile: filepath.Join(d.config.DockerCertPath, "ca.pem"),
|
||||||
CertFile: filepath.Join(d.config.DockerCertPath, "cert.pem"),
|
CertFile: filepath.Join(d.config.DockerCertPath, "cert.pem"),
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
// Copyright 2023 Harness, Inc.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package infraprovider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/harness/gitness/infraprovider/enum"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Factory struct {
|
||||||
|
providers map[enum.InfraProviderType]InfraProvider
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFactory(dockerProvider *DockerProvider) Factory {
|
||||||
|
providers := make(map[enum.InfraProviderType]InfraProvider)
|
||||||
|
providers[enum.InfraProviderTypeDocker] = dockerProvider
|
||||||
|
return Factory{providers: providers}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Factory) GetInfraProvider(providerType enum.InfraProviderType) (InfraProvider, error) {
|
||||||
|
val := f.providers[providerType]
|
||||||
|
if val == nil {
|
||||||
|
return nil, fmt.Errorf("unknown infra provider type: %s", providerType)
|
||||||
|
}
|
||||||
|
return val, nil
|
||||||
|
}
|
|
@ -23,9 +23,10 @@ import (
|
||||||
// WireSet provides a wire set for this package.
|
// WireSet provides a wire set for this package.
|
||||||
var WireSet = wire.NewSet(
|
var WireSet = wire.NewSet(
|
||||||
ProvideDockerProvider,
|
ProvideDockerProvider,
|
||||||
|
ProvideFactory,
|
||||||
)
|
)
|
||||||
|
|
||||||
func ProvideDockerProvider(config *types.Config) InfraProvider {
|
func ProvideDockerProvider(config *types.Config) *DockerProvider {
|
||||||
dockerConfig := Config{
|
dockerConfig := Config{
|
||||||
DockerHost: config.Docker.Host,
|
DockerHost: config.Docker.Host,
|
||||||
DockerAPIVersion: config.Docker.APIVersion,
|
DockerAPIVersion: config.Docker.APIVersion,
|
||||||
|
@ -34,3 +35,7 @@ func ProvideDockerProvider(config *types.Config) InfraProvider {
|
||||||
}
|
}
|
||||||
return NewDockerProvider(&dockerConfig)
|
return NewDockerProvider(&dockerConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ProvideFactory(dockerProvider *DockerProvider) Factory {
|
||||||
|
return NewFactory(dockerProvider)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue