Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChiaNetwork API and controller #181

Merged
merged 9 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ resources:
kind: ChiaCrawler
path: github.com/chia-network/chia-operator/api/v1
version: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: chia.net
group: k8s
kind: ChiaNetwork
path: github.com/chia-network/chia-operator/api/v1
version: v1
version: "3"
4 changes: 4 additions & 0 deletions api/v1/chiacommon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ type CommonSpecChia struct {
// +optional
Testnet *bool `json:"testnet,omitempty"`

// ChiaNetwork is the name of a ChiaNetwork resource in the same namespace as this resource
// +optional
ChiaNetwork *string `json:"chiaNetwork,omitempty"`

// Network can be set to a network name in the chia configuration file to switch to
// +optional
Network *string `json:"network,omitempty"`
Expand Down
125 changes: 125 additions & 0 deletions api/v1/chianetwork_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2024 Chia Network Inc.
*/

package v1

import (
"github.com/chia-network/go-chia-libs/pkg/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ChiaNetworkSpec defines the desired state of ChiaNetwork
type ChiaNetworkSpec struct {
// NetworkConstants specifies the network constants for this network in the config
// +optional
NetworkConstants *NetworkConstants `json:"constants"`

// NetworkConfig is the config for the network (address prefix and default full_node port)
// +optional
NetworkConfig *config.NetworkConfig `json:"config"`

// NetworkName is the name of the selected network in the config, and will also be used as the key for related network config and constants.
// If specified on a ChiaNetwork, and passed to a chia-deploying resource, this will override any value specified for `.spec.chia.network` on that resource.
// This field is optional, and network name will default to the ChiaNetwork name if unspecified.
// +optional
NetworkName *string `json:"networkName,omitempty"`

// NetworkPort can be set to the port that full_nodes will use in the selected network.
// If specified on a ChiaNetwork, and passed to a chia-deploying resource, this will override any value specified for `.spec.chia.networkPort` on that resource.
// +optional
NetworkPort *uint16 `json:"networkPort,omitempty"`

// IntroducerAddress can be set to the hostname or IP address of an introducer to set in the chia config.
// No port should be specified, it's taken from the value of the NetworkPort setting.
// If specified on a ChiaNetwork, and passed to a chia-deploying resource, this will override any value specified for `.spec.chia.introducerAddress` on that resource.
// +optional
IntroducerAddress *string `json:"introducerAddress,omitempty"`

// DNSIntroducerAddress can be set to a hostname to a DNS Introducer server.
// If specified on a ChiaNetwork, and passed to a chia-deploying resource, this will override any value specified for `.spec.chia.dnsIntroducerAddress` on that resource.
// +optional
DNSIntroducerAddress *string `json:"dnsIntroducerAddress,omitempty"`
}

// NetworkConstants the constants for each network
type NetworkConstants struct {
GenesisChallenge string `json:"GENESIS_CHALLENGE"`
GenesisPreFarmPoolPuzzleHash string `json:"GENESIS_PRE_FARM_POOL_PUZZLE_HASH"`
GenesisPreFarmFarmerPuzzleHash string `json:"GENESIS_PRE_FARM_FARMER_PUZZLE_HASH"`

// +optional
AggSigMeAdditionalData string `json:"AGG_SIG_ME_ADDITIONAL_DATA,omitempty"`

// TODO this should actually be a uint128 but it's much more difficult to implement a custom uint128 type with controller-tools generating CRDs yamls
// +optional
DifficultyConstantFactor uint64 `json:"DIFFICULTY_CONSTANT_FACTOR,omitempty"`

// +optional
DifficultyStarting uint64 `json:"DIFFICULTY_STARTING,omitempty"`

// +optional
EpochBlocks uint32 `json:"EPOCH_BLOCKS,omitempty"`

// +optional
MempoolBlockBuffer uint8 `json:"MEMPOOL_BLOCK_BUFFER,omitempty"`

// +optional
MinPlotSize uint8 `json:"MIN_PLOT_SIZE,omitempty"`

// +optional
NetworkType uint8 `json:"NETWORK_TYPE,omitempty"`

// +optional
SubSlotItersStarting uint64 `json:"SUB_SLOT_ITERS_STARTING,omitempty"`

// +optional
HardForkHeight uint32 `json:"HARD_FORK_HEIGHT,omitempty"`

// +optional
SoftFork4Height uint32 `json:"SOFT_FORK4_HEIGHT,omitempty"`

// +optional
SoftFork5Height uint32 `json:"SOFT_FORK5_HEIGHT,omitempty"`

// +optional
PlotFilter128Height uint32 `json:"PLOT_FILTER_128_HEIGHT,omitempty"`

// +optional
PlotFilter64Height uint32 `json:"PLOT_FILTER_64_HEIGHT,omitempty"`

// +optional
PlotFilter32Height uint32 `json:"PLOT_FILTER_32_HEIGHT,omitempty"`
}

// ChiaNetworkStatus defines the observed state of ChiaNetwork
type ChiaNetworkStatus struct {
// Ready says whether the ChiaNetwork is ready, which should be true when the ConfigMap is created
// +kubebuilder:default=false
Ready bool `json:"ready,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ChiaNetwork is the Schema for the chianetworks API
type ChiaNetwork struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ChiaNetworkSpec `json:"spec,omitempty"`
Status ChiaNetworkStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ChiaNetworkList contains a list of ChiaNetwork
type ChiaNetworkList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ChiaNetwork `json:"items"`
}

func init() {
SchemeBuilder.Register(&ChiaNetwork{}, &ChiaNetworkList{})
}
96 changes: 96 additions & 0 deletions api/v1/chianetwork_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2024 Chia Network Inc.
*/

package v1

import (
"testing"

"github.com/chia-network/go-chia-libs/pkg/config"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

func TestUnmarshalChiaNetwork(t *testing.T) {
yamlData := []byte(`
apiVersion: k8s.chia.net/v1
kind: ChiaNetwork
metadata:
labels:
app.kubernetes.io/name: chianetwork
app.kubernetes.io/instance: chianetwork-sample
app.kubernetes.io/part-of: chia-operator
app.kubernetes.io/created-by: chia-operator
name: chianetwork-sample
spec:
constants:
MIN_PLOT_SIZE: 18
GENESIS_CHALLENGE: fb00c54298fc1c149afbf4c8996fb2317ae41e4649b934ca495991b7852b841
GENESIS_PRE_FARM_POOL_PUZZLE_HASH: asdlsakldlskalskdsasdasdsadsadsadsadsdsadsas
GENESIS_PRE_FARM_FARMER_PUZZLE_HASH: testestestestestestestesrestestestestestest
config:
address_prefix: txch
default_full_node_port: 58444
networkName: testnetz
networkPort: 58444
introducerAddress: intro.testnetz.example.com
dnsIntroducerAddress: dnsintro.testnetz.example.com
`)

var (
networkConfig = config.NetworkConfig{
AddressPrefix: "txch",
DefaultFullNodePort: 58444,
}
networkConsts = NetworkConstants{
MinPlotSize: 18,
GenesisChallenge: "fb00c54298fc1c149afbf4c8996fb2317ae41e4649b934ca495991b7852b841",
GenesisPreFarmPoolPuzzleHash: "asdlsakldlskalskdsasdasdsadsadsadsadsdsadsas",
GenesisPreFarmFarmerPuzzleHash: "testestestestestestestesrestestestestestest",
}
network = "testnetz"
networkPort uint16 = 58444
introducerAddress = "intro.testnetz.example.com"
dnsIntroducerAddress = "dnsintro.testnetz.example.com"
)

expect := ChiaNetwork{
TypeMeta: metav1.TypeMeta{
APIVersion: "k8s.chia.net/v1",
Kind: "ChiaNetwork",
},
ObjectMeta: metav1.ObjectMeta{
Name: "chianetwork-sample",
Labels: map[string]string{
"app.kubernetes.io/name": "chianetwork",
"app.kubernetes.io/instance": "chianetwork-sample",
"app.kubernetes.io/part-of": "chia-operator",
"app.kubernetes.io/created-by": "chia-operator",
},
},
Spec: ChiaNetworkSpec{
NetworkConfig: &networkConfig,
NetworkConstants: &networkConsts,
NetworkName: &network,
NetworkPort: &networkPort,
IntroducerAddress: &introducerAddress,
DNSIntroducerAddress: &dnsIntroducerAddress,
},
}

var actual ChiaNetwork
err := yaml.Unmarshal(yamlData, &actual)
if err != nil {
t.Errorf("Error unmarshaling yaml: %v", err)
return
}

diff := cmp.Diff(actual, expect)
if diff != "" {
t.Errorf("Unmarshaled struct does not match the expected struct. Actual: %+v\nExpected: %+v\nDiff: %s", actual, expect, diff)
return
}
}
Loading