...

Source file src/edge-infra.dev/pkg/edge/datasync/apis/v1alpha1/server.go

Documentation: edge-infra.dev/pkg/edge/datasync/apis/v1alpha1

     1  package v1alpha1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"edge-infra.dev/pkg/edge/datasync/couchdb"
     7  	nodemeta "edge-infra.dev/pkg/sds/ien/node"
     8  
     9  	corev1 "k8s.io/api/core/v1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  )
    12  
    13  // +kubebuilder:object;generate=false
    14  // +k8s:deepcopy-gen=false
    15  type ServerRef interface {
    16  	ServerRef() ServerReference
    17  }
    18  
    19  const (
    20  	seedFormat = "couchdb@%s-%d.%s.%s.svc.cluster.local"
    21  	uriFormat  = "%s.%s.svc.cluster.local"
    22  )
    23  
    24  var (
    25  	maxDBsOpen     = 600
    26  	httpAddress    = "any"
    27  	requireAuth    = true
    28  	additionalPort = true
    29  	metricsPort    = 17986
    30  	changeTimout   = 60000
    31  	numberOfShards = 4
    32  	editSecurityDB = true
    33  )
    34  
    35  // NewAdminCouchDBServer TODO: delete banners with old clusters
    36  func NewAdminCouchDBServer() *CouchDBServer {
    37  	// TODO(help_wanted) - no idea why we use this, shouldn't be using the same value everywhere
    38  	uid := "13c6c660a78e493db1eff008751538ec"
    39  	validUser := false
    40  	seedList := seedList(couchdb.Namespace, couchdb.AdminNodeCount)
    41  	adminSecretRef := corev1.SecretReference{
    42  		Name:      couchdb.AdminSecretName,
    43  		Namespace: couchdb.Namespace,
    44  	}
    45  	ratio := &SmooshChannelConfig{
    46  		Priority:    ptr("ratio"),
    47  		MinPriority: ptr(5.0),
    48  		MinSize:     ptr(float64(50000000)),
    49  		Concurrency: ptr(1),
    50  	}
    51  	slack := &SmooshChannelConfig{
    52  		Priority:    ptr("slack"),
    53  		MinPriority: ptr(float64(200000000)),
    54  		MinSize:     ptr(float64(50000000)),
    55  		Concurrency: ptr(1),
    56  	}
    57  
    58  	return &CouchDBServer{
    59  		TypeMeta: metav1.TypeMeta{
    60  			APIVersion: GroupVersion.String(),
    61  			Kind:       "CouchDBServer",
    62  		},
    63  		ObjectMeta: metav1.ObjectMeta{
    64  			Name:      couchdb.AdminServerName,
    65  			Namespace: couchdb.Namespace,
    66  			Labels: map[string]string{
    67  				couchdb.NodeUIDLabel: uid,
    68  			},
    69  			Annotations: map[string]string{
    70  				couchdb.StatefulSetLabel: couchdb.Name,
    71  			},
    72  		},
    73  		Spec: CouchDBServerSpec{
    74  			DiskMonitor: &DiskMonitor{
    75  				Enable: ptr(true),
    76  			},
    77  			Type: Cloud,
    78  			URI:  fmt.Sprintf(uriFormat, couchdb.Namespace, couchdb.Namespace),
    79  			Admin: Admin{
    80  				Credentials: adminSecretRef,
    81  				Cookie: corev1.SecretReference{
    82  					Name: couchdb.CookieSecretName,
    83  				},
    84  				ReplicationSecret: ReplicationSecret{
    85  					Target:     couchdb.ReplicationSMgrSecretName,
    86  					FromSecret: adminSecretRef,
    87  				},
    88  			},
    89  			Ingress: Ingress{
    90  				Certificate: Certificate{
    91  					Name: couchdb.CouchCertName,
    92  				},
    93  			},
    94  			Cluster: Cluster{
    95  				Nodes:      couchdb.AdminNodeCount,
    96  				AutoFinish: true,
    97  			},
    98  			Base: &BaseConfig{
    99  				UUID:             &uid,
   100  				MaxDBsOpen:       &maxDBsOpen,
   101  				SecurityEditable: &editSecurityDB,
   102  			},
   103  			Server: &ServerConfig{
   104  				HTTP: &HTTPConfig{
   105  					BindAddress:      &httpAddress,
   106  					RequireValidUser: &validUser,
   107  					ChangesTimeout:   &changeTimout,
   108  				},
   109  			},
   110  			Clustering: &ClusterConfig{
   111  				Shards:   ptr(numberOfShards),
   112  				SeedList: seedList,
   113  			},
   114  			Log: &LogConfig{
   115  				Level: ptr(Warning),
   116  			},
   117  			Smoosh: &SmooshConfig{
   118  				DBChannels:      []string{"upgrade_dbs", "ratio_dbs", "slack_dbs"},
   119  				ViewChannels:    []string{"upgrade_views", "ratio_views", "slack_views"},
   120  				CleanupChannels: []string{"index_cleanup"},
   121  				// for more info: https://docs.couchdb.org/en/stable/config/compaction.html#smoosh.%3Cchannel%3E
   122  				Channels: map[string]*SmooshChannelConfig{
   123  					"ratio_dbs":   ratio,
   124  					"ratio_views": ratio,
   125  					"slack_dbs":   slack,
   126  					"slack_views": slack,
   127  				},
   128  			},
   129  			IOQ: &IOQConfig{
   130  				Bypass: &IOQBypassConfig{
   131  					OSProcess:  ptr(true),
   132  					Read:       ptr(true),
   133  					Write:      ptr(true),
   134  					ViewUpdate: ptr(true),
   135  					ShardSync:  ptr(false),
   136  					Compaction: ptr(false),
   137  					Reshard:    ptr(false),
   138  				},
   139  			},
   140  			Metrics: &MetricsConfig{
   141  				AdditionalPort: &additionalPort,
   142  				BindAddress:    &httpAddress,
   143  				Port:           &metricsPort,
   144  			},
   145  		},
   146  	}
   147  }
   148  
   149  func NewStoreCouchDBServer() *CouchDBServer {
   150  	// data-sync-couchdb-0.data-sync-couchdb.data-sync-couchdb.svc.cluster.local
   151  	var (
   152  		uid         = "18ca6776db334e1493fd4d3a00bf373f"
   153  		singleNode  = true
   154  		maxHistory  = 7
   155  		connTimeout = 90000
   156  	)
   157  	podURI := fmt.Sprintf("%s-0.%s", couchdb.Name, couchdb.URI)
   158  	adminSecretRef := corev1.SecretReference{
   159  		Name:      couchdb.StoreSecretName,
   160  		Namespace: couchdb.Namespace,
   161  	}
   162  
   163  	ratio := &SmooshChannelConfig{
   164  		Priority:    ptr("ratio"),
   165  		MinPriority: ptr(1.3),
   166  		MinSize:     ptr(float64(10000)),
   167  		Concurrency: ptr(5),
   168  	}
   169  	slack := &SmooshChannelConfig{
   170  		Priority:    ptr("slack"),
   171  		MinPriority: ptr(float64(20000000)),
   172  		MinSize:     ptr(float64(10000)),
   173  		Concurrency: ptr(5),
   174  	}
   175  	// create couchdbserver resource on the store
   176  	return &CouchDBServer{
   177  		TypeMeta: metav1.TypeMeta{
   178  			APIVersion: GroupVersion.String(),
   179  			Kind:       "CouchDBServer",
   180  		},
   181  		ObjectMeta: metav1.ObjectMeta{
   182  			Name:      couchdb.StoreServerName,
   183  			Namespace: couchdb.Namespace,
   184  			Labels: map[string]string{
   185  				couchdb.NodeUIDLabel: uid,
   186  			},
   187  			Annotations: map[string]string{
   188  				couchdb.StatefulSetLabel: couchdb.Name,
   189  			},
   190  		},
   191  		Spec: CouchDBServerSpec{
   192  			DiskMonitor: &DiskMonitor{
   193  				Enable: ptr(true),
   194  			},
   195  			Type: Store,
   196  			URI:  podURI,
   197  			Admin: Admin{
   198  				Credentials: adminSecretRef,
   199  				ReplicationSecret: ReplicationSecret{
   200  					FromSecret: adminSecretRef,
   201  				},
   202  			},
   203  			Cluster: Cluster{
   204  				Nodes:      couchdb.StoreNodeCount,
   205  				AutoFinish: true,
   206  			},
   207  			Base: &BaseConfig{
   208  				UUID:             &uid,
   209  				SingleNode:       &singleNode,
   210  				MaxDBsOpen:       &maxDBsOpen,
   211  				DefaultSecurity:  DefaultSecurityAdminOnly,
   212  				Compression:      CompressionDeflate1,
   213  				SecurityEditable: &editSecurityDB,
   214  			},
   215  			Clustering: &ClusterConfig{
   216  				Shards: ptr(2),
   217  				Nodes:  ptr(couchdb.StoreNodeCount),
   218  			},
   219  			Log: &LogConfig{
   220  				Level: ptr(Warning),
   221  			},
   222  			Server: &ServerConfig{
   223  				HTTP: &HTTPConfig{
   224  					BindAddress:              &httpAddress,
   225  					RequireValidUserExceptUp: &requireAuth,
   226  				},
   227  			},
   228  			Replicator: &ReplicatorConfig{
   229  				MaxHistory:         &maxHistory,
   230  				ConnectionTimeout:  &connTimeout,
   231  				MaxJobs:            ptr(50),
   232  				Interval:           ptr(60000),
   233  				MaxChurn:           ptr(10),
   234  				CheckpointInterval: ptr(300000),
   235  				UseCheckpoints:     ptr(true),
   236  				UseBulkGet:         ptr(true),
   237  			},
   238  			Smoosh: &SmooshConfig{
   239  				DBChannels:         []string{"upgrade_dbs", "ratio_dbs", "slack_dbs"},
   240  				ViewChannels:       []string{"upgrade_views", "ratio_views", "slack_views"},
   241  				CleanupChannels:    []string{"index_cleanup"},
   242  				CompactionLogLevel: ptr(Debug),
   243  				// for more info: https://docs.couchdb.org/en/stable/config/compaction.html#smoosh.%3Cchannel%3E
   244  				Channels: map[string]*SmooshChannelConfig{
   245  					"ratio_dbs":   ratio,
   246  					"ratio_views": ratio,
   247  					"slack_dbs":   slack,
   248  					"slack_views": slack,
   249  				},
   250  			},
   251  			Metrics: &MetricsConfig{
   252  				AdditionalPort: &additionalPort,
   253  				BindAddress:    &httpAddress,
   254  				Port:           &metricsPort,
   255  			},
   256  		},
   257  	}
   258  }
   259  
   260  func NewTouchpointCouchDBServer(uid, laneNumber string) *CouchDBServer {
   261  	podURI := fmt.Sprintf("%s-%s-0.%s", couchdb.Name, laneNumber, couchdb.URI)
   262  	name := fmt.Sprintf("%s-%s", couchdb.Name, uid)
   263  	server := NewStoreCouchDBServer()
   264  	server.Labels[nodemeta.LaneLabel] = laneNumber
   265  	server.Labels[couchdb.NodeUIDLabel] = uid
   266  	server.Annotations[couchdb.StatefulSetLabel] = fmt.Sprintf("%s-%s", couchdb.Name, laneNumber)
   267  	server.Name = name
   268  	server.Spec.Type = Touchpoint
   269  	server.Spec.URI = podURI
   270  	server.Spec.Cluster.AutoFinish = false
   271  	return server
   272  }
   273  
   274  func NewCouchDBUser(t UserType, name, serverName string, roles ...string) *CouchDBUser {
   275  	return &CouchDBUser{
   276  		TypeMeta: metav1.TypeMeta{
   277  			APIVersion: GroupVersion.String(),
   278  			Kind:       "CouchDBUser",
   279  		},
   280  		ObjectMeta: metav1.ObjectMeta{
   281  			Name:      name,
   282  			Namespace: couchdb.Namespace,
   283  		},
   284  		Spec: CouchDBUserSpec{
   285  			Type: t,
   286  			ServerRef: ServerReference{
   287  				Name:      serverName,
   288  				Namespace: couchdb.Namespace,
   289  			},
   290  			User: User{
   291  				//Name is optional, couchctl will create random user.
   292  				Roles: roles,
   293  			},
   294  		},
   295  	}
   296  }
   297  
   298  func seedList(namespace string, nodeCount int) []string {
   299  	seeds := make([]string, nodeCount)
   300  	for i := 0; i < nodeCount; i++ {
   301  		seeds[i] = fmt.Sprintf(seedFormat, namespace, i, namespace, namespace)
   302  	}
   303  	return seeds
   304  }
   305  
   306  func ptr[T comparable](t T) *T {
   307  	return &t
   308  }
   309  

View as plain text