This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
41 changed files
with
2,418 additions
and
2,002 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,184 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
mqttapi "github.com/rancher/octopus/pkg/mqtt/api" | ||
) | ||
|
||
// MQTTDevicePropertyType defines the type of the property value. | ||
// +kubebuilder:validation:Enum=int;string;float;boolean;object;array | ||
type MQTTDevicePropertyType string | ||
|
||
const ( | ||
// Device Property value type | ||
ValueTypeInt ValueType = "int" | ||
ValueTypeString ValueType = "string" | ||
ValueTypeFloat ValueType = "float" | ||
ValueTypeBoolean ValueType = "boolean" | ||
ValueTypeArray ValueType = "array" | ||
ValueTypeObject ValueType = "object" | ||
|
||
// Subscribed topic payload type | ||
PayloadTypeJSON PayloadType = "json" | ||
MQTTDevicePropertyTypeInt MQTTDevicePropertyType = "int" | ||
MQTTDevicePropertyTypeString MQTTDevicePropertyType = "string" | ||
MQTTDevicePropertyTypeFloat MQTTDevicePropertyType = "float" | ||
MQTTDevicePropertyTypeBoolean MQTTDevicePropertyType = "boolean" | ||
MQTTDevicePropertyTypeObject MQTTDevicePropertyType = "object" | ||
MQTTDevicePropertyTypeArray MQTTDevicePropertyType = "array" | ||
) | ||
|
||
// Defines the type of the property value. | ||
// +kubebuilder:validation:Enum=int;string;float;boolean;array;object | ||
type ValueType string | ||
// MQTTDevicePattern defines the pattern that published/subscribed the message. | ||
// AttributedMessage: Compress properties into one message, one topic has its all property values. | ||
// AttributedTopic: Flatten properties to topic, each topic has its own property value. | ||
// +kubebuilder:validation:Enum=AttributedMessage;AttributedTopic | ||
type MQTTDevicePattern string | ||
|
||
// The payload type type. | ||
// +kubebuilder:validation:Enum=json | ||
type PayloadType string | ||
const ( | ||
MQTTDevicePatternAttributedMessage MQTTDevicePattern = "AttributedMessage" | ||
MQTTDevicePatternAttributeTopic MQTTDevicePattern = "AttributedTopic" | ||
) | ||
|
||
// The qos type. | ||
// +kubebuilder:validation:Enum=0;1;2 | ||
type QosType int | ||
// MQTTDeviceSchema defines the pattern schema. | ||
type MQTTDeviceSchema struct { | ||
// Specifies the type of schema. | ||
// +optional | ||
Type string `json:"type,omitempty"` | ||
|
||
type MqttConfig struct { | ||
Broker string `json:"broker"` | ||
Username string `json:"username,omitempty"` | ||
Password string `json:"password,omitempty"` | ||
// Specifies the reference for schema. | ||
// +optional | ||
Reference string `json:"reference,omitempty"` | ||
} | ||
|
||
type SubInfo struct { | ||
Topic string `json:"topic"` | ||
PayloadType PayloadType `json:"payloadType"` | ||
Qos QosType `json:"qos"` | ||
} | ||
// MQTTDeviceProtocol is the Schema for configuring the protocol of MQTTDevice. | ||
type MQTTDeviceProtocol struct { | ||
mqttapi.MQTTOptions `json:",inline"` | ||
|
||
type PubInfo struct { | ||
Topic string `json:"topic"` | ||
Qos QosType `json:"qos"` | ||
} | ||
// Specifies the pattern of MQTTDevice protocol. | ||
// +kubebuilder:validation:Required | ||
Pattern MQTTDevicePattern `json:"pattern"` | ||
|
||
type ValueFloat struct { | ||
F float64 `json:"-"` | ||
// Specifies the schema of the pattern. | ||
// +optional | ||
Schema *MQTTDeviceSchema `json:"schema,omitempty"` | ||
} | ||
|
||
func (v *ValueFloat) MarshalJSON() ([]byte, error) { | ||
str := fmt.Sprintf(`"%f"`, v.F) | ||
return []byte(str), nil | ||
// MQTTDevicePropertyValue defines the value of the property. | ||
// +kubebuilder:validation:Type="" | ||
// +kubebuilder:validation:XPreserveUnknownFields | ||
type MQTTDevicePropertyValue struct { | ||
Raw []byte `json:"-"` | ||
} | ||
func (v *ValueFloat) UnmarshalJSON(value []byte) error { | ||
var err error | ||
s := value | ||
if len(s) > 0 && s[0] == '"' { | ||
s = s[1:] | ||
|
||
// UnmarshalJSON implements the json.Unmarshaller interface. | ||
func (in *MQTTDevicePropertyValue) UnmarshalJSON(data []byte) error { | ||
if len(data) > 0 && string(data) != "null" { | ||
in.Raw = data | ||
} | ||
if len(s) > 0 && s[len(s)-1] == '"' { | ||
s = s[:len(s)-1] | ||
return nil | ||
} | ||
|
||
// MarshalJSON implements the json.Marshaler interface. | ||
func (in MQTTDevicePropertyValue) MarshalJSON() ([]byte, error) { | ||
if len(in.Raw) > 0 { | ||
return in.Raw, nil | ||
} | ||
v.F, err = strconv.ParseFloat(string(s), 64) | ||
return err | ||
return nil, nil | ||
} | ||
|
||
type ValueArrayProps struct { | ||
// +kubebuilder:validation:XPreserveUnknownFields | ||
ValueProps `json:",inline"` | ||
// OpenAPISchemaType is used by the kube-openapi generator when constructing | ||
// the OpenAPI spec of this type. | ||
// See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators | ||
func (MQTTDevicePropertyValue) OpenAPISchemaType() []string { | ||
// TODO: return actual types when anyOf is supported | ||
return nil | ||
} | ||
|
||
type ValueProps struct { | ||
// Reports the type of property. | ||
ValueType ValueType `json:"valueType"` | ||
// OpenAPISchemaFormat is used by the kube-openapi generator when constructing | ||
// the OpenAPI spec of this type. | ||
func (MQTTDevicePropertyValue) OpenAPISchemaFormat() string { return "" } | ||
|
||
// Reports the value of int type. | ||
// +optional | ||
IntValue int64 `json:"intValue,omitempty"` | ||
// MQTTDeviceProperty defines the specified property of MQTTDevice. | ||
type MQTTDeviceProperty struct { | ||
mqttapi.MQTTMessagePayloadOptions `json:",inline"` | ||
mqttapi.MQTTMessageTopicOperation `json:",inline"` | ||
|
||
// Reports the value of string type. | ||
// Specifies the annotations of property. | ||
// +optional | ||
StringValue string `json:"stringValue,omitempty"` | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
|
||
// Reports the value of float type. | ||
// +optional | ||
FloatValue *ValueFloat `json:"floatValue,omitempty"` | ||
// Specifies the name of property. | ||
// +kubebuilder:validation:Required | ||
Name string `json:"name"` | ||
|
||
// Reports the value of boolean type. | ||
// Specifies the description of property. | ||
// +optional | ||
BooleanValue bool `json:"booleanValue,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
|
||
// Reports the value of array type. | ||
// Specifies the type of property. | ||
// +kubebuilder:validation:Required | ||
Type MQTTDevicePropertyType `json:"type,omitempty"` | ||
|
||
// Specifies the value of property. | ||
// +optional | ||
ArrayValue []ValueArrayProps `json:"arrayValue,omitempty"` | ||
Value *MQTTDevicePropertyValue `json:"value,omitempty"` | ||
|
||
// Reports the value of object type. | ||
// +kubebuilder:validation:XPreserveUnknownFields | ||
// Specifies the MIME of property value. | ||
// +optional | ||
ObjectValue *runtime.RawExtension `json:"objectValue,omitempty"` | ||
} | ||
ContentType string `json:"contentType,omitempty"` | ||
|
||
type Property struct { | ||
SubInfo SubInfo `json:"subInfo"` | ||
PubInfo PubInfo `json:"pubInfo,omitempty"` | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
JSONPath string `json:"jsonPath"` | ||
Value ValueProps `json:"value,omitempty"` | ||
// Specifies if the property is read-only. | ||
// The default value is "true". | ||
// +kubebuilder:default=true | ||
ReadOnly *bool `json:"readOnly,omitempty"` | ||
} | ||
|
||
type StatusProperty struct { | ||
Name string `json:"name"` | ||
Description string `json:"description,omitempty"` | ||
Value ValueProps `json:"value"` | ||
UpdatedAt metav1.Time `json:"updateAt"` | ||
// MQTTDeviceStatusProperty defines the observed property of MQTTDevice. | ||
type MQTTDeviceStatusProperty struct { | ||
MQTTDeviceProperty `json:",inline"` | ||
|
||
// Reports the updated timestamp of property. | ||
// +optional | ||
UpdatedAt *metav1.Time `json:"updateAt,omitempty"` | ||
} | ||
|
||
// MqttDeviceSpec defines the desired state of MqttDevice | ||
type MqttDeviceSpec struct { | ||
Config MqttConfig `json:"config"` | ||
Properties []Property `json:"properties"` | ||
// MQTTDeviceSpec defines the desired state of MQTTDevice. | ||
type MQTTDeviceSpec struct { | ||
// Specifies the protocol for accessing the MQTT service. | ||
// +kubebuilder:validation:Required | ||
Protocol MQTTDeviceProtocol `json:"protocol"` | ||
|
||
// Specifies the properties of MQTTDevice. | ||
// +listType=map | ||
// +listMapKey=name | ||
// +optional | ||
Properties []MQTTDeviceProperty `json:"properties,omitempty"` | ||
} | ||
|
||
// MqttDeviceStatus defines the observed state of MqttDevice | ||
type MqttDeviceStatus struct { | ||
Properties []StatusProperty `json:"properties"` | ||
// MQTTDeviceStatus defines the observed state of MQTTDevice. | ||
type MQTTDeviceStatus struct { | ||
// Reports the properties of MQTTDevice. | ||
// +optional | ||
Properties []MQTTDeviceStatusProperty `json:"properties,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
// +k8s:openapi-gen=true | ||
// +kubebuilder:resource:shortName=mqtt | ||
// +kubebuilder:subresource:status | ||
// MqttDevice is the Schema for the mqtt device API | ||
type MqttDevice struct { | ||
// +kubebuilder:printcolumn:name="PATTERN",type="string",JSONPath=`.spec.protocol.pattern` | ||
// +kubebuilder:printcolumn:name="SERVER",type="string",JSONPath=`.spec.protocol.client.server` | ||
// +kubebuilder:printcolumn:name="TOPIC",type="string",JSONPath=`.spec.protocol.message.topic` | ||
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=`.metadata.creationTimestamp` | ||
// MQTTDevice is the Schema for the MQTT device API. | ||
type MQTTDevice struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec MqttDeviceSpec `json:"spec,omitempty"` | ||
// +kubebuilder:validation:XPreserveUnknownFields | ||
Status MqttDeviceStatus `json:"status,omitempty"` | ||
Spec MQTTDeviceSpec `json:"spec,omitempty"` | ||
Status MQTTDeviceStatus `json:"status,omitempty"` | ||
} | ||
|
||
// +kubebuilder:object:root=true | ||
// MqttDeviceList contains a list of MqttDevice | ||
type MqttDeviceList struct { | ||
// MQTTDeviceList contains a list of MQTTDevice. | ||
type MQTTDeviceList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata,omitempty"` | ||
Items []MqttDevice `json:"items"` | ||
|
||
Items []MQTTDevice `json:"items"` | ||
} | ||
|
||
func init() { | ||
SchemeBuilder.Register(&MqttDevice{}, &MqttDeviceList{}) | ||
SchemeBuilder.Register(&MQTTDevice{}, &MQTTDeviceList{}) | ||
} |
Oops, something went wrong.