common

package
v0.0.0-...-d3e8332 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2024 License: BSD-3-Clause Imports: 19 Imported by: 0

README

Common

The common package contains functions and structs that are used by multiple services

Documentation

Overview

Package common provides functions and structs used by multiple Cacao services for interacting with Kubernetes clusters, NATS queues, and other services

Index

Constants

This section is empty.

Variables

View Source
var MonitoringSubject = "Phylax.CreateSubscriber"

MonitoringSubject is the subject phylax monitors for creating subscribers

Functions

func CreateCloudEvent

func CreateCloudEvent(request []byte, eventType string, source string, id string) ([]byte, error)

CreateCloudEvent takes a JSON byte slice and uses it as the data for a new CloudEvent object Marshaled as a JSON byte slice

func CreateResourceName

func CreateResourceName(username, name, id string) string

CreateResourceName is used to create a K8s resource name that is both unique and informative to users:

<username:limit to 8>-<name:limit to 32 chars>-<xid>

func FixName

func FixName(name string) string

FixName will make a string K8s-valid by making it lowercase and hyphenated

func GetNATSInfo

func GetNATSInfo(defaultClusterID string, defaultClientID string, defaultConnAddress string) map[string]string

GetNATSInfo returns a map containing necessary NATS connection variables. It is used to get this information from environment variables or use the defaults

func GetRequestFromCloudEvent

func GetRequestFromCloudEvent(msg []byte) (dataBytes []byte, err error)

GetRequestFromCloudEvent will unmarshal a CloudEvent object and unmarshal the data field into JSON bytes

func PtrOf

func PtrOf[T any](data T) *T

PtrOf return a pointer to the data.

Useful for converting constants and literal, e.g. PtrOf(true), PtrOf("foobar")

func PublishRequest

func PublishRequest(request Request, subject string, source string, connAddress string) (result []byte, err error)

PublishRequest can be used to create a CloudEvent and publish it on NATS. It expects a reply

func RegisterMonitoring

func RegisterMonitoring(subject string, natsInfo map[string]string)

RegisterMonitoring is used to monitor a separate streaming channel

func StanQueueSubscriber

func StanQueueSubscriber(subject string, queue string, cb stan.MsgHandler, natsInfo map[string]string, wg *sync.WaitGroup)

StanQueueSubscriber creates a NATS Streaming Subscriber that will listen on a subject as a queue subscriber and perform asynchronous actions. The cb parameter can by any function which conforms to the stan.MsgHandler type (i.e. accepts *stan.Msg as a parameter). For information on queue subscribers, see https://docs.nats.io/developing-with-nats-streaming/queues

func StanSubscriber

func StanSubscriber(subject string, cb stan.MsgHandler, natsInfo map[string]string, wg *sync.WaitGroup)

StanSubscriber creates a NATS Streaming Subscriber that will listen on a subject and perform asynchronous actions. The cb parameter can by any function which conforms to the stan.MsgHandler type (i.e. accepts *stan.Msg as a parameter)

func StreamingPublish

func StreamingPublish(request []byte, natsInfo map[string]string, subject string, source string) error

StreamingPublish can be used to create a CloudEvent and publish it on NATS Streaming

func StreamingQueueSubscriber

func StreamingQueueSubscriber(subject string, queue string, action func([]byte, map[string]string), natsInfo map[string]string, wg *sync.WaitGroup)

StreamingQueueSubscriber creates a NATS Streaming Queue Subscriber that will listen on a subject and perform asynchronous actions. The action function should accept a []byte that represents JSON data, and a map[string]string, which represents NATS connection info.

func StreamingSubscriber

func StreamingSubscriber(subject string, action func([]byte, map[string]string), natsInfo map[string]string, wg *sync.WaitGroup)

StreamingSubscriber creates a NATS Streaming Subscriber that will listen on a subject and perform asynchronous actions. The action function should accept a []byte that represents JSON data, and a map[string]string, which represents NATS connection info.

func StreamingTestSubscriber

func StreamingTestSubscriber(subject string, queue string, action func([]byte, map[string]string), natsInfo map[string]string, wg *sync.WaitGroup)

StreamingTestSubscriber is the same as StreamingSubscriber but will exit after receiving and processing one request

func SynchronousSubscriber

func SynchronousSubscriber(subject string, queue string, action func([]byte, *nats.Msg, map[string]string), natsInfo map[string]string, wg *sync.WaitGroup)

SynchronousSubscriber is used to synchronously respond to requests

func SynchronousTestSubscriber

func SynchronousTestSubscriber(subject string, queue string, action func([]byte, *nats.Msg, map[string]string), natsInfo map[string]string, wg *sync.WaitGroup)

SynchronousTestSubscriber is the same as SynchronousSubscriber but will exit after receiving and processing one request

Types

type BasicRequest

type BasicRequest struct {
	User             User   `json:"user,omitempty"`
	ClusterID        string `json:"cluster_id,omitempty"`
	RegistrySecretID string `json:"registry_secret_id,omitempty"`
	GitSecretID      string `json:"git_secret_id,omitempty"`
	WorkflowID       string `json:"workflow_id,omitempty"`
	BuildID          string `json:"build_id,omitempty"`
	RunID            string `json:"run_id,omitempty"`

	Error Error `json:"error,omitempty"`
}

BasicRequest is the simplest implementation of the Request interface and is intended to be included as an embedded/anonymous struct in extending types

func (*BasicRequest) GetBuildID

func (r *BasicRequest) GetBuildID() string

GetBuildID ...

func (*BasicRequest) GetClusterID

func (r *BasicRequest) GetClusterID() string

GetClusterID ...

func (*BasicRequest) GetError

func (r *BasicRequest) GetError() Error

GetError ...

func (*BasicRequest) GetGitSecretID

func (r *BasicRequest) GetGitSecretID() string

GetGitSecretID ...

func (*BasicRequest) GetRedacted

func (r *BasicRequest) GetRedacted() BasicRequest

GetRedacted will return a new BasicRequest with the confidential fields removed from the User's Secrets and Clusters

func (*BasicRequest) GetRegistrySecretID

func (r *BasicRequest) GetRegistrySecretID() string

GetRegistrySecretID ...

func (*BasicRequest) GetRunID

func (r *BasicRequest) GetRunID() string

GetRunID ...

func (*BasicRequest) GetUser

func (r *BasicRequest) GetUser() User

GetUser ...

func (*BasicRequest) GetWorkflowID

func (r *BasicRequest) GetWorkflowID() string

GetWorkflowID ...

func (*BasicRequest) SetBuildID

func (r *BasicRequest) SetBuildID(value string)

SetBuildID ...

func (*BasicRequest) SetClusterID

func (r *BasicRequest) SetClusterID(value string)

SetClusterID ...

func (*BasicRequest) SetGitSecretID

func (r *BasicRequest) SetGitSecretID(value string)

SetGitSecretID ...

func (*BasicRequest) SetRegistrySecretID

func (r *BasicRequest) SetRegistrySecretID(value string)

SetRegistrySecretID ...

func (*BasicRequest) SetRunID

func (r *BasicRequest) SetRunID(value string)

SetRunID ...

func (*BasicRequest) SetUser

func (r *BasicRequest) SetUser(value User)

SetUser ...

func (*BasicRequest) SetWorkflowID

func (r *BasicRequest) SetWorkflowID(value string)

SetWorkflowID ...

type BuildStatus

type BuildStatus struct {
	Complete bool   `json:"complete"`
	Success  bool   `json:"success"`
	Info     string `json:"info"`
}

BuildStatus is used to get information about a Build. Info will be a container's output log

type BuildStep

type BuildStep struct {
	Image      string        `bson:"image" json:"image,omitempty" yaml:"image,omitempty"`
	Dockerfile string        `bson:"dockerfile" json:"dockerfile,omitempty" yaml:"dockerfile,omitempty"`
	Args       []core.EnvVar `bson:"args" json:"args,omitempty" yaml:"args,omitempty"`
}

BuildStep ...

type CRUD

type CRUD interface {
	GetMock() *mock.Mock
	Create(string, CacaoBase) error
	Read(string, string) ([]byte, error)
	Update(string, CacaoBase) error
	Replace(string, CacaoBase) error
	Delete(string, string) error
	ReadFor(string, string, string) ([]map[string]interface{}, error)
	ReadForUser(string, string) ([]map[string]interface{}, error)
}

CRUD is an interface that allows for mocking of the MongoDB

type CacaoBase

type CacaoBase interface {
	GetID() string
	GetCollection() string
	GetOwner() string
}

CacaoBase is a base interface type for Cacao entities that need to be stored

type Cluster

type Cluster struct {
	ID               string `json:"id,omitempty"`
	Name             string `json:"name,omitempty"`
	DefaultNamespace string `json:"default_namespace,omitempty"`
	Config           string `json:"config,omitempty"`
	Host             string `json:"host,omitempty"`
	Slug             string `json:"slug,omitempty"`
	Admin            bool   `json:"admin,omitempty"`
}

Cluster ...

func (*Cluster) GetRedacted

func (c *Cluster) GetRedacted() Cluster

GetRedacted will return a new Cluster with the Value set to "REDACTED"

type DatabaseAdapter

type DatabaseAdapter struct {
	*mongo.Database
}

DatabaseAdapter implements the CRUD interface to allow for mocking

func GetMongoDBAdapter

func GetMongoDBAdapter(name, address string) (adapter DatabaseAdapter, err error)

GetMongoDBAdapter returns a new connection to MongoDB and wraps it in a CRUD interface

func (DatabaseAdapter) Create

func (d DatabaseAdapter) Create(collectionName string, document CacaoBase) (err error)

Create will insert into the database

func (DatabaseAdapter) Delete

func (d DatabaseAdapter) Delete(collectionName, id string) (err error)

Delete will delete from the database

func (DatabaseAdapter) GetMock

func (d DatabaseAdapter) GetMock() (m *mock.Mock)

GetMock ...

func (DatabaseAdapter) Read

func (d DatabaseAdapter) Read(collectionName string, id string) (data []byte, err error)

Read will read from the database

func (DatabaseAdapter) ReadFor

func (d DatabaseAdapter) ReadFor(collectionName, key, value string) (results []map[string]interface{}, err error)

ReadFor will return all objects with a key

func (DatabaseAdapter) ReadForUser

func (d DatabaseAdapter) ReadForUser(collectionName, owner string) (results []map[string]interface{}, err error)

ReadForUser will return all objects owned by a user

func (DatabaseAdapter) Replace

func (d DatabaseAdapter) Replace(collectionName string, document CacaoBase) (err error)

Replace will save/replace an existing object in the database

func (DatabaseAdapter) Update

func (d DatabaseAdapter) Update(collectionName string, document CacaoBase) (err error)

Update wil update/edit in the database

type Error

type Error struct {
	Message string `json:"message,omitempty"`
	Code    int    `json:"code,omitempty"`
}

Error is used for responding to synchronous requests with an error that is JSON friendly

type MockedAdapter

type MockedAdapter struct {
	Mock *mock.Mock
}

MockedAdapter for testing

func (MockedAdapter) Create

func (d MockedAdapter) Create(collectionName string, document CacaoBase) error

Create ...

func (MockedAdapter) Delete

func (d MockedAdapter) Delete(collectionName, id string) error

Delete ...

func (MockedAdapter) GetMock

func (d MockedAdapter) GetMock() *mock.Mock

GetMock ...

func (MockedAdapter) Read

func (d MockedAdapter) Read(collectionName, id string) ([]byte, error)

Read ...

func (MockedAdapter) ReadFor

func (d MockedAdapter) ReadFor(collectionName, key, value string) ([]map[string]interface{}, error)

ReadFor ...

func (MockedAdapter) ReadForUser

func (d MockedAdapter) ReadForUser(collectionName, owner string) ([]map[string]interface{}, error)

ReadForUser ...

func (MockedAdapter) Replace

func (d MockedAdapter) Replace(collectionName string, document CacaoBase) error

Replace ...

func (MockedAdapter) Update

func (d MockedAdapter) Update(collectionName string, document CacaoBase) error

Update ...

type Repository

type Repository struct {
	RawURL string `bson:"rawurl" json:"rawurl,omitempty"`
	URL    string `bson:"url" json:"url,omitempty"`
	Branch string `bson:"branch" json:"branch,omitempty"`
}

Repository contains basic information required to interact with git The URL is the RawURL without the leading `https://` in order to use as an ID and the RawURL is saved for cloning

type Request

type Request interface {
	GetClusterID() string
	GetRegistrySecretID() string
	GetGitSecretID() string
	GetWorkflowID() string
	GetBuildID() string
	GetRunID() string
	GetUser() User
	GetError() Error

	SetClusterID(value string)
	SetRegistrySecretID(value string)
	SetGitSecretID(value string)
	SetWorkflowID(value string)
	SetBuildID(value string)
	SetRunID(value string)
	SetUser(value User)
}

Request is an interface that allows for reading common HTTP request fields and can be extended by service-specific types for reading additional fields

type Secret

type Secret struct {
	Username string     `json:"username,omitempty"`
	Value    string     `json:"value,omitempty"`
	Type     SecretType `json:"type,omitempty"`
	ID       string     `json:"id,omitempty"`
}

Secret ... Deprecated: Secret is deprecated. Use Service.credentialModel in cacao-common instead

type SecretType

type SecretType string

SecretType associates a type with a Secret

const (
	// GCRSecret is used for Google Container Registry
	GCRSecret SecretType = "gcr"
	// DockerhubSecret is used for Dockerhub
	DockerhubSecret SecretType = "dockerhub"
	// GitSecret is used for Github, GitLab, etc.
	GitSecret SecretType = "git"
)

type User

type User struct {
	Username string             `json:"username,omitempty"`
	Password string             `json:"password,omitempty,omitempty"`
	Secrets  map[string]Secret  `json:"secrets,omitempty"`
	Clusters map[string]Cluster `json:"clusters,omitempty"`
	IsAdmin  bool               `json:"is_admin"`
}

User defines user attributes, primarily Secrets that are written to Vault. Note that the Password field is only used for creating the User

func (*User) GetRedacted

func (u *User) GetRedacted() User

GetRedacted will return a new User with sensitive fields redacted

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL