Skip to content

Commit

Permalink
update the client of iotdock to deploy edgex according to different v…
Browse files Browse the repository at this point in the history
…ersions

Signed-off-by: wangxye <[email protected]>
  • Loading branch information
wangxye committed Aug 21, 2023
1 parent b6f2ec0 commit 4046d24
Show file tree
Hide file tree
Showing 18 changed files with 123 additions and 31 deletions.
15 changes: 9 additions & 6 deletions cmd/yurt-iot-dock/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
"github.com/openyurtio/openyurt/pkg/apis"
edgexclients "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)
Expand Down Expand Up @@ -116,15 +117,17 @@ func Run(opts *options.YurtIoTDockOptions, stopCh <-chan struct{}) {
}
}

edgexdock := edgexclients.NewEdgexDock(opts.Version, opts.CoreMetadataAddr, opts.CoreCommandAddr)

// setup the DeviceProfile Reconciler and Syncer
if err = (&controllers.DeviceProfileReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, opts); err != nil {
}).SetupWithManager(mgr, opts, edgexdock); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DeviceProfile")
os.Exit(1)
}
dfs, err := controllers.NewDeviceProfileSyncer(mgr.GetClient(), opts)
dfs, err := controllers.NewDeviceProfileSyncer(mgr.GetClient(), opts, edgexdock)
if err != nil {
setupLog.Error(err, "unable to create syncer", "syncer", "DeviceProfile")
os.Exit(1)
Expand All @@ -139,11 +142,11 @@ func Run(opts *options.YurtIoTDockOptions, stopCh <-chan struct{}) {
if err = (&controllers.DeviceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, opts); err != nil {
}).SetupWithManager(mgr, opts, edgexdock); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Device")
os.Exit(1)
}
ds, err := controllers.NewDeviceSyncer(mgr.GetClient(), opts)
ds, err := controllers.NewDeviceSyncer(mgr.GetClient(), opts, edgexdock)
if err != nil {
setupLog.Error(err, "unable to create syncer", "controller", "Device")
os.Exit(1)
Expand All @@ -158,11 +161,11 @@ func Run(opts *options.YurtIoTDockOptions, stopCh <-chan struct{}) {
if err = (&controllers.DeviceServiceReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, opts); err != nil {
}).SetupWithManager(mgr, opts, edgexdock); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DeviceService")
os.Exit(1)
}
dss, err := controllers.NewDeviceServiceSyncer(mgr.GetClient(), opts)
dss, err := controllers.NewDeviceServiceSyncer(mgr.GetClient(), opts, edgexdock)
if err != nil {
setupLog.Error(err, "unable to create syncer", "syncer", "DeviceService")
os.Exit(1)
Expand Down
3 changes: 3 additions & 0 deletions cmd/yurt-iot-dock/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type YurtIoTDockOptions struct {
EnableLeaderElection bool
Nodepool string
Namespace string
Version string
CoreDataAddr string
CoreMetadataAddr string
CoreCommandAddr string
Expand All @@ -43,6 +44,7 @@ func NewYurtIoTDockOptions() *YurtIoTDockOptions {
EnableLeaderElection: false,
Nodepool: "",
Namespace: "default",
Version: "",
CoreDataAddr: "edgex-core-data:59880",
CoreMetadataAddr: "edgex-core-metadata:59881",
CoreCommandAddr: "edgex-core-command:59882",
Expand All @@ -63,6 +65,7 @@ func (o *YurtIoTDockOptions) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&o.EnableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+"Enabling this will ensure there is only one active controller manager.")
fs.StringVar(&o.Nodepool, "nodepool", "", "The nodePool deviceController is deployed in.(just for debugging)")
fs.StringVar(&o.Namespace, "namespace", "default", "The cluster namespace for edge resources synchronization.")
fs.StringVar(&o.Version, "version", "", "The version of edge resources deploymenet.")
fs.StringVar(&o.CoreDataAddr, "core-data-address", "edgex-core-data:59880", "The address of edge core-data service.")
fs.StringVar(&o.CoreMetadataAddr, "core-metadata-address", "edgex-core-metadata:59881", "The address of edge core-metadata service.")
fs.StringVar(&o.CoreCommandAddr, "core-command-address", "edgex-core-command:59882", "The address of edge core-command service.")
Expand Down
54 changes: 54 additions & 0 deletions pkg/yurtiotdock/clients/edgex-foundry/edgexobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,60 @@ limitations under the License.

package edgex_foundry

import (
"fmt"

"github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
edgexCliv2 "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry/v2"
)

type EdgeXObject interface {
IsAddedToEdgeX() bool
}

type EdgexDock struct {
Version string
CoreMetadataAddr string
CoreCommandAddr string
}

func NewEdgexDock(version string, coreMetadataAddr string, coreCommandAddr string) *EdgexDock {
return &EdgexDock{
Version: version,
CoreMetadataAddr: coreMetadataAddr,
CoreCommandAddr: coreCommandAddr,
}
}

func (ep *EdgexDock) CreateDeviceClient() (clients.DeviceInterface, error) {
switch ep.Version {
case "minesota":
return nil, nil
case "levski", "kamakura", "jakarta":
return edgexCliv2.NewEdgexDeviceClient(ep.CoreMetadataAddr, ep.CoreCommandAddr), nil
default:
return nil, fmt.Errorf("unsupported Edgex version: %v", ep.Version)
}
}

func (ep *EdgexDock) CreateDeviceProfileClient() (clients.DeviceProfileInterface, error) {
switch ep.Version {
case "minesota":
return nil, nil
case "levski", "kamakura", "jakarta":
return edgexCliv2.NewEdgexDeviceProfile(ep.CoreMetadataAddr), nil
default:
return nil, fmt.Errorf("unsupported Edgex version: %v", ep.Version)
}
}

func (ep *EdgexDock) CreateDeviceServiceClient() (clients.DeviceServiceInterface, error) {
switch ep.Version {
case "minesota":
return nil, nil
case "levski", "kamakura", "jakarta":
return edgexCliv2.NewEdgexDeviceServiceClient(ep.CoreMetadataAddr), nil
default:
return nil, fmt.Errorf("unsupported Edgex version: %v", ep.Version)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package edgex_foundry
package v2

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 edgex_foundry
package v2

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package edgex_foundry
package v2

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 edgex_foundry
package v2

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package edgex_foundry
package v2

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 edgex_foundry
package v2

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package edgex_foundry
package v2

import (
"fmt"
Expand Down
7 changes: 7 additions & 0 deletions pkg/yurtiotdock/clients/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,10 @@ type DeviceProfileInterface interface {
Get(ctx context.Context, name string, options GetOptions) (*iotv1alpha1.DeviceProfile, error)
List(ctx context.Context, options ListOptions) ([]iotv1alpha1.DeviceProfile, error)
}

// IoTDock defines the interfaces which used to create deviceclient, deviceprofileclient, deviceserviceclient
type IoTDock interface {
CreateDeviceClient() (DeviceInterface, error)
CreateDeviceProfileClient() (DeviceProfileInterface, error)
CreateDeviceServiceClient() (DeviceServiceInterface, error)
}
10 changes: 7 additions & 3 deletions pkg/yurtiotdock/controllers/device_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
edgexCli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
edgexobj "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
util "github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)

Expand Down Expand Up @@ -109,8 +109,12 @@ func (r *DeviceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

// SetupWithManager sets up the controller with the Manager.
func (r *DeviceReconciler) SetupWithManager(mgr ctrl.Manager, opts *options.YurtIoTDockOptions) error {
r.deviceCli = edgexCli.NewEdgexDeviceClient(opts.CoreMetadataAddr, opts.CoreCommandAddr)
func (r *DeviceReconciler) SetupWithManager(mgr ctrl.Manager, opts *options.YurtIoTDockOptions, edgexdock *edgexobj.EdgexDock) error {
deviceclient, err := edgexdock.CreateDeviceClient()
if err != nil {
return err
}
r.deviceCli = deviceclient
r.NodePool = opts.Nodepool
r.Namespace = opts.Namespace

Expand Down
10 changes: 7 additions & 3 deletions pkg/yurtiotdock/controllers/device_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
edgeCli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
efCli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
edgexobj "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)

Expand All @@ -46,10 +46,14 @@ type DeviceSyncer struct {
}

// NewDeviceSyncer initialize a New DeviceSyncer
func NewDeviceSyncer(client client.Client, opts *options.YurtIoTDockOptions) (DeviceSyncer, error) {
func NewDeviceSyncer(client client.Client, opts *options.YurtIoTDockOptions, edgexdock *edgexobj.EdgexDock) (DeviceSyncer, error) {
devicelient, err := edgexdock.CreateDeviceClient()
if err != nil {
return DeviceSyncer{}, err
}
return DeviceSyncer{
syncPeriod: time.Duration(opts.EdgeSyncPeriod) * time.Second,
deviceCli: efCli.NewEdgexDeviceClient(opts.CoreMetadataAddr, opts.CoreCommandAddr),
deviceCli: devicelient,
Client: client,
NodePool: opts.Nodepool,
Namespace: opts.Namespace,
Expand Down
10 changes: 7 additions & 3 deletions pkg/yurtiotdock/controllers/deviceprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
edgexclis "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
edgexobj "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)

Expand Down Expand Up @@ -87,8 +87,12 @@ func (r *DeviceProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

// SetupWithManager sets up the controller with the Manager.
func (r *DeviceProfileReconciler) SetupWithManager(mgr ctrl.Manager, opts *options.YurtIoTDockOptions) error {
r.edgeClient = edgexclis.NewEdgexDeviceProfile(opts.CoreMetadataAddr)
func (r *DeviceProfileReconciler) SetupWithManager(mgr ctrl.Manager, opts *options.YurtIoTDockOptions, edgexdock *edgexobj.EdgexDock) error {
deviceprofileclient, err := edgexdock.CreateDeviceProfileClient()
if err != nil {
return err
}
r.edgeClient = deviceprofileclient
r.NodePool = opts.Nodepool
r.Namespace = opts.Namespace

Expand Down
10 changes: 7 additions & 3 deletions pkg/yurtiotdock/controllers/deviceprofile_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
devcli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
edgexclis "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
edgexobj "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)

Expand All @@ -45,10 +45,14 @@ type DeviceProfileSyncer struct {
}

// NewDeviceProfileSyncer initialize a New DeviceProfileSyncer
func NewDeviceProfileSyncer(client client.Client, opts *options.YurtIoTDockOptions) (DeviceProfileSyncer, error) {
func NewDeviceProfileSyncer(client client.Client, opts *options.YurtIoTDockOptions, edgexdock *edgexobj.EdgexDock) (DeviceProfileSyncer, error) {
edgeclient, err := edgexdock.CreateDeviceProfileClient()
if err != nil {
return DeviceProfileSyncer{}, err
}
return DeviceProfileSyncer{
syncPeriod: time.Duration(opts.EdgeSyncPeriod) * time.Second,
edgeClient: edgexclis.NewEdgexDeviceProfile(opts.CoreMetadataAddr),
edgeClient: edgeclient,
Client: client,
NodePool: opts.Nodepool,
Namespace: opts.Namespace,
Expand Down
10 changes: 7 additions & 3 deletions pkg/yurtiotdock/controllers/deviceservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
edgexCli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
edgexobj "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
util "github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)

Expand Down Expand Up @@ -107,8 +107,12 @@ func (r *DeviceServiceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

// SetupWithManager sets up the controller with the Manager.
func (r *DeviceServiceReconciler) SetupWithManager(mgr ctrl.Manager, opts *options.YurtIoTDockOptions) error {
r.deviceServiceCli = edgexCli.NewEdgexDeviceServiceClient(opts.CoreMetadataAddr)
func (r *DeviceServiceReconciler) SetupWithManager(mgr ctrl.Manager, opts *options.YurtIoTDockOptions, edgexdock *edgexobj.EdgexDock) error {
deviceserviceclient, err := edgexdock.CreateDeviceServiceClient()
if err != nil {
return err
}
r.deviceServiceCli = deviceserviceclient
r.NodePool = opts.Nodepool
r.Namespace = opts.Namespace

Expand Down
10 changes: 7 additions & 3 deletions pkg/yurtiotdock/controllers/deviceservice_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/openyurtio/openyurt/cmd/yurt-iot-dock/app/options"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
iotcli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients"
edgexCli "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
edgexobj "github.com/openyurtio/openyurt/pkg/yurtiotdock/clients/edgex-foundry"
"github.com/openyurtio/openyurt/pkg/yurtiotdock/controllers/util"
)

Expand All @@ -42,10 +42,14 @@ type DeviceServiceSyncer struct {
Namespace string
}

func NewDeviceServiceSyncer(client client.Client, opts *options.YurtIoTDockOptions) (DeviceServiceSyncer, error) {
func NewDeviceServiceSyncer(client client.Client, opts *options.YurtIoTDockOptions, edgexdock *edgexobj.EdgexDock) (DeviceServiceSyncer, error) {
deviceserviceclient, err := edgexdock.CreateDeviceServiceClient()
if err != nil {
return DeviceServiceSyncer{}, err
}
return DeviceServiceSyncer{
syncPeriod: time.Duration(opts.EdgeSyncPeriod) * time.Second,
deviceServiceCli: edgexCli.NewEdgexDeviceServiceClient(opts.CoreMetadataAddr),
deviceServiceCli: deviceserviceclient,
Client: client,
NodePool: opts.Nodepool,
Namespace: opts.Namespace,
Expand Down
1 change: 1 addition & 0 deletions pkg/yurtmanager/controller/platformadmin/iotdock.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func newYurtIoTDockComponent(platformAdmin *iotv1alpha2.PlatformAdmin, platformA
"--metrics-bind-address=127.0.0.1:8080",
"--leader-elect=false",
fmt.Sprintf("--namespace=%s", ns),
fmt.Sprintf("--version=%s", platformAdmin.Spec.Version),
},
LivenessProbe: &corev1.Probe{
InitialDelaySeconds: 15,
Expand Down

0 comments on commit 4046d24

Please sign in to comment.